package com.qmth.sop.server.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.SysDingGroupResult;
import com.qmth.sop.business.entity.SysDingGroup;
import com.qmth.sop.business.service.SysDingGroupService;
import com.qmth.sop.business.service.SysDingObjService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.MessageTypeEnum;
import com.qmth.sop.common.enums.ServiceStatusEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Objects;

/**
 * 考勤组表 控制器.
 *
 * @author: shudonghui
 * @date: 2023-08-02 14:37:24
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "考勤组表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DING_GROUP)
public class SysDingGroupController {


    @Resource
    SysDingGroupService sysDingGroupService;

    @Resource
    SysDingObjService sysDingObjService;


    @ApiOperation(value = "考勤组表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考勤组表列表信息", response = SysDingGroupResult.class)})
    public Result query(
            @ApiParam(value = "服务单元id", required = false) @RequestParam(required = false) Long serviceId,
            @ApiParam(value = "服务状态") @RequestParam(required = false) ServiceStatusEnum status,
            @ApiParam(value = "适用供应商", required = false) @RequestParam(required = false) Long[] supplierIds,
            @ApiParam(value = "适用考勤对象", required = false) @RequestParam(required = false) Long[] dingRoleIds,
            @ApiParam(value = "审核角色", required = false) @RequestParam(required = false) Long[] approveRoleIds,
            @ApiParam(value = "是否开启人脸", required = false) @RequestParam(required = false) Boolean faceOpen,
            @ApiParam(value = "创建起始时间") @RequestParam(required = false) Long createStartTime,
            @ApiParam(value = "创建截止时间") @RequestParam(required = false) Long createEndTime,
            @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<SysDingGroupResult> resultIPage = sysDingGroupService.query(new Page<>(pageNumber, pageSize), serviceId,status,supplierIds,dingRoleIds,approveRoleIds,faceOpen,createStartTime,createEndTime);

        return ResultUtil.ok(resultIPage);
    }


    @ApiOperation(value = "新增考勤组表接口")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "新增考勤组表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.ADD)
    public Result add(@Valid @ApiParam(value = "考勤组表信息", required = true) @RequestBody(required = true) SysDingGroupResult sysDingGroup, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysDingGroupService.add(sysDingGroup);
        return ResultUtil.ok();
    }


    @ApiOperation(value = "修改考勤组表接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "修改考勤组表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result update(@Valid @ApiParam(value = "考勤组表信息", required = true) @RequestBody(required = true) SysDingGroupResult sysDingGroup, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysDingGroupService.update(sysDingGroup);
        return ResultUtil.ok();
    }


    @ApiOperation(value = "删除考勤组表接口")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除考勤组表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result delete(@ApiParam(value = "考勤组表信息", required = true) @RequestParam(required = true) long id) {
        sysDingGroupService.delete(id);
        return ResultUtil.ok();
    }

    /*列表

     */

    @ApiOperation(value = "考勤组表列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考勤组表列表", response = SysDingGroup.class, responseContainer = "List")})
    public Result list(@ApiParam(value = "查询条件", required = false) @RequestParam(required = false) Boolean enable) {
        return ResultUtil.ok(sysDingGroupService.list(new QueryWrapper<SysDingGroup>().lambda().eq(Objects.nonNull(enable), SysDingGroup::getEnable, enable)));
    }

    /**
     * 获取单个
     *
     * @param id
     * @return
     */

    @ApiOperation(value = "获取单个考勤组表信息")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考勤组表信息", response = SysDingGroupResult.class)})
    public Result get(@ApiParam(value = "id", required = true) @RequestParam(value = "id") long id) {
        SysDingGroup sysDingGroup = sysDingGroupService.getById(id);
        SysDingGroupResult sysDingGroupResult = JSON.toJavaObject((JSON) JSON.toJSON(sysDingGroup), SysDingGroupResult.class);
        sysDingGroupResult.setDingObjs(sysDingObjService.getList(id));
        return ResultUtil.ok(sysDingGroupResult);
    }


}