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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.dto.WorkerInfoDto;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerInfo;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IWorkerInfoService;
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.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @Title: Controller
 * @Description: 人员
 * @author： pengj
 * @date： 2023-03-13
 * @version： V1.0
 */
@RestController
@RequestMapping("/ent/workerInfo")
@Slf4j
@Api(tags = "人员管理")
public class EntWorkerInfoController {
    @Autowired
    private IWorkerInfoService workerInfoService;

    @Autowired
    private IEngineeringService engineeringService;
    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "分页查询", operDesc = "分页列表查询人员信息")
    @ApiOperation(value = " 分页列表查询人员信息", notes = "分页列表查询人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personType", value = "人员类别(1:管理;2:工人)", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "personName", value = "姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "inServiceType", value = "人员状态  1在场  2离场", paramType = "query", dataType = "Integer")
    })
    @PostMapping(value = "/page")
    public Result<IPage<WorkerInfoDto>> queryPageList(Page page, @RequestBody WorkerInfo workerInfo) {
        QueryWrapper<WorkerInfo> wrapper = Wrappers.<WorkerInfo>query();
//        SecurityUser user = SecurityUtil.getUser();
        if (workerInfo.getPersonType() != null) {
            wrapper.eq("w.person_type", workerInfo.getPersonType());
        }
        if (StringUtils.isNotBlank(workerInfo.getPersonName())) {
            wrapper.like("w.person_name", workerInfo.getPersonName());
        }
        if (StringUtils.isNotBlank(workerInfo.getIdCard())) {
            wrapper.like("w.id_card", workerInfo.getIdCard());
        }
        if (workerInfo.getInServiceType() != null) {
            wrapper.eq("w.in_service_type", workerInfo.getInServiceType());
        }
        if (StringUtils.isNotBlank(workerInfo.getEngineeringSn())) {
            wrapper.eq("t.engineering_sn", workerInfo.getEngineeringSn());
        } /*else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in("t.engineering_sn", engineeringSns);
            }
        }*/
        IPage<WorkerInfoDto> pageList = workerInfoService.getDetailPageList(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 列表查询
     *
     * @param workerInfo
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "列表查询", operDesc = "列表查询人员信息")
    @ApiOperation(value = " 列表查询人员信息", notes = "列表查询人员信息", httpMethod = "POST")
    @PostMapping(value = "/list")
    public Result<List<WorkerInfo>> queryList(@RequestBody WorkerInfo workerInfo) {
        LambdaQueryWrapper<WorkerInfo> wrapper = Wrappers.<WorkerInfo>lambdaQuery();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(workerInfo.getEngineeringSn())) {
            wrapper.eq(WorkerInfo::getEngineeringSn, workerInfo.getEngineeringSn());
        }/* else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in(WorkerInfo::getEngineeringSn, engineeringSns);
            }
        }*/
        List<WorkerInfo> list = workerInfoService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 添加
     *
     * @param workerInfo
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "新增", operDesc = "添加人员信息")
    @ApiOperation(value = " 添加人员信息", notes = "添加人员信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody WorkerInfo workerInfo) {
        workerInfoService.saveInfo(workerInfo);
        return Result.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param workerInfo
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "修改", operDesc = "编辑人员信息")
    @ApiOperation(value = "编辑人员信息", notes = "编辑人员信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<WorkerInfo> edit(@RequestBody WorkerInfo workerInfo) {
        Result<WorkerInfo> result = new Result<WorkerInfo>();
        WorkerInfo workerInfoEntity = workerInfoService.getById(workerInfo.getId());
        if (workerInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = workerInfoService.updateInfo(workerInfo);
            if (ok) {
                result.success("修改成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }

    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "查询", operDesc = "通过id查询人员信息")
    @ApiOperation(value = "通过id查询人员信息", notes = "通过id查询人员信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "人员ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<WorkerInfo> queryById(@RequestBody Map<String, Object> map) {
        Result<WorkerInfo> result = new Result<WorkerInfo>();
        WorkerInfo workerInfo = workerInfoService.getById(MapUtils.getString(map, "id"));
        if (workerInfo == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(workerInfo);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 批量复职
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "修改", operDesc = "批量复职")
    @ApiOperation(value = "批量复职", notes = "批量复职", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "人员ID", paramType = "query", required = true, dataType = "List")
    @PostMapping(value = "/entry")
    public Result<Object> entry(@RequestBody List<String> ids) {
        return Result.success(workerInfoService.updateInServiceType(1, ids));
    }

    /**
     * 批量离场
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "修改", operDesc = "批量离场")
    @ApiOperation(value = "批量离场", notes = "批量离场", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "人员ID", paramType = "query", required = true, dataType = "List")
    @PostMapping(value = "/exit")
    public Result<Object> exit(@RequestBody List<String> ids) {
        return Result.success(workerInfoService.updateInServiceType(2, ids));
    }

    /**
     * 考勤统计
     * @return
     */
    @OperLog(operModul = "人员管理", operType = "统计", operDesc = "考勤统计")
    @ApiOperation(value = "考勤统计", notes = "考勤统计", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personName", value = "姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "teamName", value = "班组", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", required = true, dataType = "String")
    })
    @PostMapping(value = "/attendStat")
    public Result<Page<Map<String, Object>>> attendStat(Page page, @RequestBody Map<String, Object> map) {
        return Result.success(workerInfoService.attendanceStat(page, map));
    }
}
