package com.zhgd.xmgl.modules.basicdata.controller.government;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.Engineering;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerInfo;
import com.zhgd.xmgl.modules.basicdata.service.IAttendanceRecordService;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IWorkerAttendanceService;
import com.zhgd.xmgl.modules.basicdata.service.IWorkerInfoService;
import com.zhgd.xmgl.modules.basicdata.statistics.EngineeringStat;
import com.zhgd.xmgl.modules.basicdata.statistics.WorkerInfoStat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @Title: Controller
* @Description: 人员
* @author： pengj
* @date：   2023-03-14
* @version： V1.0
*/
@RestController
@RequestMapping("/gov/workerStat")
@Slf4j
@Api(tags = "劳务实名制管理")
public class GovWorkerStatController {
   @Autowired
   private IWorkerAttendanceService workerAttendanceService;

    @Autowired
    private IWorkerInfoService workerInfoService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    /**
     * 人员出勤监管
     * @return
     */
    @OperLog(operModul = "劳务实名制管理", operType = "统计", operDesc = "人员出勤监管")
    @ApiOperation(value = " 人员出勤监管", notes = "人员出勤监管", httpMethod = "GET")
    @GetMapping(value = "/attendStatistics")
    public Result<Map<String, Object>> attendanceStatistics() {
        return Result.success(workerAttendanceService.statistics());
    }

    /**
     * 出勤统计
     * @return
     */
    @OperLog(operModul = "劳务实名制管理", operType = "统计", operDesc = "出勤统计")
    @ApiOperation(value = " 出勤统计", notes = "出勤统计", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personType", value = "人员类别(1:管理;2:工人)", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @PostMapping(value = "/attendRecord")
    public Result<List<Map<String, Object>>> attendRecordStatistics(@RequestBody Map<String, Object> map) {
        return Result.success(attendanceRecordService.filterList(map));
    }

    /**
     * 工人属性分析
     * @return
     */
    @OperLog(operModul = "劳务实名制管理", operType = "统计", operDesc = "工人属性分析")
    @ApiOperation(value = " 工人属性分析", notes = "工人属性分析", httpMethod = "GET")
    @GetMapping(value = "/attribute")
    public Result<WorkerInfoStat> workerStatistics() {
        return Result.success(workerInfoService.statistics());
    }

    /**
     * 施工项目查询
     * @return
     */
    @OperLog(operModul = "劳务实名制管理", operType = "分页查询", operDesc = "施工项目查询")
    @ApiOperation(value = " 施工项目查询", notes = "施工项目查询", httpMethod = "POST")
    @ApiImplicitParam(name = "engineeringName", value = "工程名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/engineeringPage")
    public Result<Page<EngineeringStat>> engineeringPageList(Page page, @RequestBody Engineering engineering) {
        LambdaQueryWrapper<Engineering> wrapper = Wrappers.<Engineering>lambdaQuery();
        if (StringUtils.isNotBlank(engineering.getEngineeringName())) {
            wrapper.like(Engineering::getEngineeringName, engineering.getEngineeringName());
        }
        return Result.success(engineeringService.engineeringStat(page, wrapper));
    }

    /**
     * 在册人员查询
     * @return
     */
    @OperLog(operModul = "劳务实名制管理", operType = "分页查询", operDesc = "在册人员查询")
    @ApiOperation(value = " 在册人员查询", notes = "在册人员查询", httpMethod = "POST")
    @ApiImplicitParam(name = "personName", value = "姓名", paramType = "query", dataType = "String")
    @PostMapping(value = "/workerPage")
        public Result<Page<WorkerInfo>> workerPage(Page page, @RequestBody WorkerInfo workerInfo) {
        LambdaQueryWrapper<WorkerInfo> wrapper = Wrappers.<WorkerInfo>lambdaQuery();
        if (StringUtils.isNotBlank(workerInfo.getPersonName())) {
            wrapper.like(WorkerInfo::getPersonName, workerInfo.getPersonName());
        }
//        SecurityUser user = SecurityUtil.getUser();
//        List<String> projectSns = projectService.getSnListForGov(user.getSn());
//        if (projectSns.size() > 0) {
//            wrapper.in(WorkerInfo::getProjectSn, projectSns);
//        }
        return Result.success(workerInfoService.getPageList(page, wrapper));
    }

    @OperLog(operModul = "劳务实名制管理", operType = "分页查询", operDesc = "查询在册人员详细信息")
    @ApiOperation(value = " 查询在册人员详细信息", notes = "查询在册人员详细信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "人员唯一标识", paramType = "query", dataType = "String")
    @PostMapping(value = "/queryDetailBySn")
    public Result<Map<String, Object>> queryDetailBySn(@RequestBody WorkerInfo workerInfoEntity) {
        Map<String, Object> resultMap = new HashMap<>();
        WorkerInfo workerInfo = workerInfoService.getOne(Wrappers.<WorkerInfo>lambdaQuery().eq(WorkerInfo::getId, workerInfoEntity.getId()));
        resultMap.put("workerInfo", workerInfo);
        List<WorkerInfo> historyList = workerInfoService.list(Wrappers.<WorkerInfo>lambdaQuery().eq(WorkerInfo::getIdCard, workerInfo.getIdCard()));
        resultMap.put("historyList", historyList);
        return Result.success(resultMap);
    }
}
