package com.willalp.web.controller.clockingin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.clockingin.domain.BizLeave;
import com.willalp.clockingin.service.IBizLeaveService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.flow.domain.BizLeaveImg;
import com.willalp.flow.service.IBizLeaveImgService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假Controller
 *
 * @author 一只闲鹿
 */
@RestController
@RequestMapping("/leave/leave")
@AllArgsConstructor
public class BizLeaveController extends BaseController {
    @Resource
    private IBizLeaveService bizLeaveService;

    @Resource
    private IBizLeaveImgService leaveImgService;

    /**
     * 查询请假列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizLeave bizLeave) {
        startPage();
        List<BizLeave> bizLeaves = bizLeaveService.selectBizLeaveList(bizLeave);
        List<BizLeave> bizLeaves1 = new ArrayList<>();
        bizLeaves.forEach(i -> {
            List<BizLeaveImg> leaveImgs = leaveImgService.getBaseMapper()
                    .selectList(new QueryWrapper<BizLeaveImg>()
                            .eq("biz_leave_id", i.getId()));
            i.imgUrls = leaveImgs;
            bizLeaves1.add(i);
        });
        return getDataTable(bizLeaves1);
    }

    /**
     * 导出请假列表
     */
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizLeave bizLeave) {
        List<BizLeave> list = bizLeaveService.selectBizLeaveList(bizLeave);
        ExcelUtil<BizLeave> util = new ExcelUtil<BizLeave>(BizLeave.class);
        return util.exportExcel(list, "leave");
    }

    /**
     * 获取请假详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizLeaveService.selectBizLeaveById(id));
    }

    /**
     * 新增请假
     */
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizLeave bizLeave) {
        String idStr = IdWorker.getIdStr();
        bizLeave.setId(Long.parseLong(idStr));
        bizLeaveService.insertBizLeave(bizLeave);
        return submitApply(Long.valueOf(idStr), bizLeave.getCandidate());
    }

    /**
     * 修改请假
     */
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizLeave bizLeave) {
        return toAjax(bizLeaveService.updateBizLeave(bizLeave));
    }

    /**
     * 删除请假
     */
    @Log(title = "请假", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizLeaveService.deleteBizLeaveByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable Long id, String c) {
        try {
            BizLeave bizLeave = bizLeaveService.selectBizLeaveById(id);
            bizLeave.setCandidate(c);
            Map<String, Object> map = new HashMap<>();
            map.put("day", Double.valueOf(bizLeave.getTotalTime()));
            bizLeaveService.updateBizLeave(bizLeave);
        } catch (Exception e) {
            e.printStackTrace();
            return error("提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
