package com.xueyi.system.staff.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyi.common.core.utils.core.StrUtil;
import com.xueyi.common.core.utils.poi.ExcelUtil;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.common.web.utils.DateUtils;
import com.xueyi.system.api.organize.domain.po.SysDeptPo;
import com.xueyi.system.organize.mapper.SysDeptMapper;
import com.xueyi.system.staff.domain.dto.DmEmpAttendanceDto;
import com.xueyi.system.staff.domain.model.DmEmpAttendanceConverter;
import com.xueyi.system.staff.domain.po.DmEmpAttendancePo;
import com.xueyi.system.staff.domain.query.DmEmpAttendanceQuery;
import com.xueyi.system.staff.mapper.DmEmpAttendanceMapper;
import com.xueyi.system.staff.service.IDmEmpAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工考勤管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/attendance")
public class DmEmpAttendanceController extends BaseController<DmEmpAttendanceQuery, DmEmpAttendanceDto, IDmEmpAttendanceService> {

    @Autowired
    private DmEmpAttendanceMapper mapper;

    @Autowired
    private DmEmpAttendanceConverter converter;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "员工考勤" ;
    }

    /**
     * 查询员工考勤列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_LIST)
    public AjaxResult list(DmEmpAttendanceQuery dmEmpAttendance) {
        return super.list(dmEmpAttendance);
    }

    /**
     * 查询员工考勤详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_SINGLE)
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.getInfo(id);
    }

    /**
     * 员工考勤新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_ADD)
    @Log(title = "员工考勤管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody DmEmpAttendanceDto dmEmpAttendance) {
        return super.add(dmEmpAttendance);
    }

    /**
     * 员工考勤修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_EDIT)
    @Log(title = "员工考勤管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody DmEmpAttendanceDto dmEmpAttendance) {
        return super.edit(dmEmpAttendance);
    }

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 员工考勤导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_EXPORT)
    @Log(title = "员工考勤管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, DmEmpAttendanceQuery dmEmpAttendance) {
//        super.export(response, dmEmpAttendance);
        ExcelUtil<DmEmpAttendanceDto> util = new ExcelUtil<>(DmEmpAttendanceDto.class);
        QueryWrapper<DmEmpAttendancePo> query = new QueryWrapper<DmEmpAttendancePo>();
        try {
            if (dmEmpAttendance != null && dmEmpAttendance.getETimestamp() != null &&dmEmpAttendance.getETimestamp()>0) {
                String sTime =  DateUtils.formatDate(DateUtils.parseLongToDate(dmEmpAttendance.getSTimestamp()),"yyyy-MM-dd");
                String eTime =  DateUtils.formatDate(DateUtils.parseLongToDate(dmEmpAttendance.getETimestamp()),"yyyy-MM-dd");
                query.between("date_str",sTime, eTime);
            }
            if (dmEmpAttendance != null && dmEmpAttendance.getDeptId() !=null && dmEmpAttendance.getDeptId() > 0) {
                query.eq("dept_id", dmEmpAttendance.getDeptId());
            }
            List<DmEmpAttendancePo> list = mapper.selectList(query);
            List<DmEmpAttendanceDto> dtos = new ArrayList<>(list.size());
            list.forEach(item ->{
                DmEmpAttendanceDto dto = converter.mapperDto(item);
                dto = dto.fixExcel();
                dtos.add(dto);
                SysDeptPo deptPo = deptMapper.selectById(item.getDeptId());
                if (deptPo!=null) {
                    dto.setDeptName(deptPo.getName());
                }
            });
            util.exportExcel(response, dtos, StrUtil.format("{}数据", getNodeName()));

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }



    }


    /**
     * 员工考勤批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.DM_EMP_ATTENDANCE_DEL)
    @Log(title = "员工考勤管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取员工考勤选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 员工考勤管理 - 列表 */
        String DM_EMP_ATTENDANCE_LIST = "attendance:attendance:list";
        /** 系统 - 员工考勤管理 - 详情 */
        String DM_EMP_ATTENDANCE_SINGLE = "attendance:attendance:single";
        /** 系统 - 员工考勤管理 - 新增 */
        String DM_EMP_ATTENDANCE_ADD = "attendance:attendance:add";
        /** 系统 - 员工考勤管理 - 修改 */
        String DM_EMP_ATTENDANCE_EDIT = "attendance:attendance:edit";
        /** 系统 - 员工考勤管理 - 导出 */
        String DM_EMP_ATTENDANCE_EXPORT = "attendance:attendance:export";
        /** 系统 - 员工考勤管理 - 删除 */
        String DM_EMP_ATTENDANCE_DEL = "attendance:attendance:delete";
    }
}
