package cn.iocoder.yudao.module.system.controller.admin.laborcontract;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractRespVO;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.SigningDateRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.laborcontract.LaborContractDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.laborcontract.LaborContractService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 劳动合同管理")
@RestController
@RequestMapping("/system/labor-contract")
@Validated
public class LaborContractController {

    @Resource
    private LaborContractService laborContractService;

    @Resource
    private AdminUserService userService;

    @Resource
    private DeptService deptService;

    @PostMapping("/create")
    @Operation(summary = "创建劳动合同管理")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:create')")
    public CommonResult<Long> createLaborContract(@Valid @RequestBody LaborContractSaveReqVO createReqVO) {
        return success(laborContractService.createLaborContract(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新劳动合同管理")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:update')")
    public CommonResult<Boolean> updateLaborContract(@Valid @RequestBody LaborContractSaveReqVO updateReqVO) {
        laborContractService.updateLaborContract(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除劳动合同管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:labor-contract:delete')")
    public CommonResult<Boolean> deleteLaborContract(@RequestParam("id") Long id) {
        laborContractService.deleteLaborContract(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得劳动合同管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:query')")
    public CommonResult<LaborContractRespVO> getLaborContract(@RequestParam("id") Long id) {
        LaborContractDO laborContract = laborContractService.getLaborContract(id);
        return success(BeanUtils.toBean(laborContract, LaborContractRespVO.class));
    }

    @GetMapping("/get-history")
    @Operation(summary = "获得用户历史签约信息")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "146")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:query')")
    public CommonResult<List<LaborContractRespVO>> getHistoryList(@RequestParam("userId") Long userId) {

        List<LaborContractDO> laborContracts = laborContractService.getHistoryList(userId);

        // 拼接数据
        AdminUserDO userDO = userService.getUser(userId);
        DeptDO deptDO = deptService.getDept(userDO.getDeptId());

        List<LaborContractRespVO> respVOS = BeanUtils.toBean(laborContracts, LaborContractRespVO.class);
        respVOS.forEach(item -> {
            item.setUserName(userDO.getNickname());
            item.setDeptName(deptDO.getName());
        });

        return success(respVOS);
    }

    @GetMapping("/page")
    @Operation(summary = "获得劳动合同管理分页")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:query')")
    public CommonResult<PageResult<LaborContractRespVO>> getLaborContractPage(@Valid LaborContractPageReqVO pageReqVO) {

        return success(laborContractService.getLaborContractPage(pageReqVO));
    }

    @GetMapping("/getSigningDate")
    @Operation(summary = "获得当前登录用户的入职时间和转正时间")
    public CommonResult<SigningDateRespVO> getSigningDate() {

        LaborContractDO contractDO = laborContractService.getSigningDate(getLoginUserId());
        if (contractDO == null) {
            return success(new SigningDateRespVO());
        }

        SigningDateRespVO respVO = BeanUtils.toBean(contractDO, SigningDateRespVO.class);
        // 设置转正日期
        LocalDate regularDate = contractDO.getSigningDate().plusMonths(contractDO.getProbationPeriodTime());
        respVO.setRegularDate(regularDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return success(respVO);
    }
}