package com.bonus.hn.bearing.capacity.backstage.real.controller;

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ExcelUtils;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import com.bonus.hn.bearing.capacity.backstage.real.entity.SmzBean;
import com.bonus.hn.bearing.capacity.backstage.real.entity.SmzPersonnelRosterBean;
import com.bonus.hn.bearing.capacity.backstage.real.service.RealDataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xliu
 * @date 2023/11/6 15:35  实名制数据展示
 */
@RestController
@RequestMapping("/realData")
public class RealDataController {
    @Resource
    private RealDataService service;

    @LogAnnotation
    @PostMapping("getPersonRosterList")
    @ApiOperation(value = "实名制数据展示-人员花名册")
    public R getPersonRosterList(SmzPersonnelRosterBean o){
        List<SmzPersonnelRosterBean> list = service.getPersonRosterList(o);
        return !list.isEmpty() ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    /**
     * 实名制人员花名册导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
    @ApiOperation(value = "实名制人员花名册-导出")
    public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SmzPersonnelRosterBean b = new SmzPersonnelRosterBean();
        b.setKeyWord(request.getParameter("keyWord"));
        b.setOrgId(request.getParameter("orgId"));
        b.setSunUnit(request.getParameter("sunUnit"));
        b.setProName(request.getParameter("proName"));
        List<SmzPersonnelRosterBean> list = service.getPersonRosterList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分公司","所在工程","姓名","性别","民族"
                ,"年龄","联系方式","身份证","分包单位","所在班组","工种","进场日期","出场日期"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "实名制人员花名册";
        if (list != null) {
            int i = 0;
            for (SmzPersonnelRosterBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分公司",bean.getRealOrgName());
                map.put("所在工程",bean.getRealProName());
                map.put("姓名",bean.getPersonName());
                map.put("性别",bean.getSex());
                map.put("民族",bean.getNation());
                map.put("年龄",bean.getAge());
                map.put("联系方式",bean.getPhone());
                map.put("身份证",bean.getIdCard());
                map.put("分包单位",bean.getSunUnit());
                map.put("所在班组",bean.getTeamName());
                map.put("工种",bean.getWorkType());
                map.put("进场日期",bean.getInTime());
                map.put("出场日期",bean.getOutTime());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @LogAnnotation
    @PostMapping("getPersonAttendanceList")
    @ApiOperation(value = "实名制数据展示-人员考勤")
    public R getPersonAttendanceList(SmzBean o){
        List<SmzBean> list = service.getPersonAttendanceList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    /**
     * 实名制人员考勤导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResults", method = RequestMethod.GET)
    @ApiOperation(value = "实名制人员考勤-导出")
    public void exportPayrolls(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SmzBean b = new SmzBean();
        b.setKeyWord(request.getParameter("keyWord"));
        b.setOrgId(request.getParameter("orgId"));
        b.setJobStatus(request.getParameter("jobStatus"));
        b.setStartTime(request.getParameter("startTime"));
        b.setEndTime(request.getParameter("endTime"));
        List<SmzBean> list = service.getPersonAttendanceList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","身份证","工种","在场/在职状态","考勤天数"
                ,"缺勤天数","所属单位/分包","所属工程","人员类型"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "实名制人员考勤";
        if (list != null) {
            int i = 0;
            for (SmzBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("身份证",bean.getIdCard());
                map.put("工种",bean.getWorkType());
                map.put("在场/在职状态",bean.getJobStatus());
                map.put("考勤天数",bean.getKqNums());
                map.put("缺勤天数",bean.getQqNums());
                map.put("所属单位/分包",bean.getSunUnit());
                map.put("所属工程",bean.getProName());
                map.put("人员类型",bean.getPersonType());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
