package cn.iocoder.yudao.module.smartfactory.controller.admin.industrialinjury;

import cn.hutool.core.collection.CollUtil;
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.smartfactory.controller.admin.industrialinjury.vo.IndustrialInjuryPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.industrialinjury.vo.IndustrialInjuryRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.industrialinjury.vo.IndustrialInjurySaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.industrialinjury.IndustrialInjuryDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService;
import cn.iocoder.yudao.module.smartfactory.service.industrialinjury.IndustrialInjuryService;
import cn.iocoder.yudao.module.smartfactory.service.staff.StaffService;
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.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 厂区工伤")
@RestController
@RequestMapping("/smartfactory/industrial-injury")
@Validated
public class IndustrialInjuryController {

    @Resource
    private IndustrialInjuryService industrialInjuryService;

    @Resource
    private StaffService staffService;

    @Resource
    private FactoryInfoService factoryInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建厂区工伤")
    @PreAuthorize("@ss.hasPermission('smartfactory:industrial-injury:create')")
    public CommonResult<Long> createIndustrialInjury(@Valid @RequestBody IndustrialInjurySaveReqVO createReqVO) {
        return success(industrialInjuryService.createIndustrialInjury(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新厂区工伤")
    @PreAuthorize("@ss.hasPermission('smartfactory:industrial-injury:update')")
    public CommonResult<Boolean> updateIndustrialInjury(@Valid @RequestBody IndustrialInjurySaveReqVO updateReqVO) {
        industrialInjuryService.updateIndustrialInjury(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除厂区工伤")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:industrial-injury:delete')")
    public CommonResult<Boolean> deleteIndustrialInjury(@RequestParam("id") Long id) {
        industrialInjuryService.deleteIndustrialInjury(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得厂区工伤")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:industrial-injury:query')")
    public CommonResult<IndustrialInjuryRespVO> getIndustrialInjury(@RequestParam("id") Long id) {
        IndustrialInjuryDO industrialInjury = industrialInjuryService.getIndustrialInjury(id);
        return success(BeanUtils.toBean(industrialInjury, IndustrialInjuryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得厂区工伤分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:industrial-injury:query')")
    public CommonResult<PageResult<IndustrialInjuryRespVO>> getIndustrialInjuryPage(@Valid IndustrialInjuryPageReqVO pageReqVO) {
        PageResult<IndustrialInjuryDO> pageResult = industrialInjuryService.getIndustrialInjuryPage(pageReqVO);
        PageResult<IndustrialInjuryRespVO> result = BeanUtils.toBean(pageResult, IndustrialInjuryRespVO.class);
        if (CollUtil.isNotEmpty(result.getList())) {

            // 获取员工信息
            Set<Long> userIds = convertSet(result.getList(), IndustrialInjuryRespVO::getStaffId);
            Map<Long, StaffDO> userMap = convertMap(staffService.getList(userIds), StaffDO::getId);

            // 获取工厂信息
            Set<Long> factoryIds = convertSet(result.getList(), IndustrialInjuryRespVO::getFactoryId);
            Map<Long, FactoryInfoDO> factoryMap = convertMap(factoryInfoService.getFactoryList(factoryIds), FactoryInfoDO::getId);

            result.getList().forEach(vo -> {
                vo.setStaffName(userMap.get(vo.getStaffId()) != null ? userMap.get(vo.getStaffId()).getNickName() : null);
                vo.setFactoryName(factoryMap.get(vo.getFactoryId()) != null ? factoryMap.get(vo.getFactoryId()).getName() : null);
                // 设置理赔总金额
                vo.setClaimTotalAmount(vo.getClaimAmount().add(vo.getAgreementClaimAmount()));
            });
        }

        return success(result);
    }
}