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

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.api.factoryInfo.FactoryInfoApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.smartfactory.api.staff.StaffApi;
import cn.iocoder.yudao.module.smartfactory.api.staff.dto.StaffDTO;
import cn.iocoder.yudao.module.system.controller.admin.loan.vo.LoanPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.loan.vo.LoanRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.loan.LoanDO;
import cn.iocoder.yudao.module.system.service.loan.LoanService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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("/system/loan")
@Validated
public class LoanController {

    @Resource
    private LoanService loanService;

    @Resource
    private StaffApi staffApi;

    @Resource
    private FactoryInfoApi factoryInfoApi;

    @GetMapping("/get")
    @Operation(summary = "获得借支管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:loan:query')")
    public CommonResult<LoanRespVO> getLoan(@RequestParam("id") Long id) {
        LoanDO loan = loanService.getLoan(id);
        return success(BeanUtils.toBean(loan, LoanRespVO.class));
    }

    @GetMapping("/getByStaffId")
    @Operation(summary = "获得指定员工的借支管理")
    @Parameter(name = "staffId", description = "员工编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:loan:query')")
    public CommonResult<LoanRespVO> getByStaffId(@RequestParam("id") Long id) {
        LoanDO loan = loanService.getByUserId(id, 1);
        return success(BeanUtils.toBean(loan, LoanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得借支管理分页")
    @PreAuthorize("@ss.hasPermission('system:loan:query')")
    public CommonResult<PageResult<LoanRespVO>> getLoanPage(@Valid LoanPageReqVO pageReqVO) {
        PageResult<LoanDO> pageResult = loanService.getLoanPage(pageReqVO);
        PageResult<LoanRespVO> result = BeanUtils.toBean(pageResult, LoanRespVO.class);

        if (CollUtil.isNotEmpty(result.getList())) {

            // 获取员工信息
            Set<Long> userIds = convertSet(result.getList(), LoanRespVO::getUserId);
            Map<Long, StaffDTO> userMap = convertMap(staffApi.getStaffList(userIds).getCheckedData(), StaffDTO::getId);

            // 获取部门信息
            Set<Long> deptIds = convertSet(result.getList(), LoanRespVO::getDeptId);
            Map<Long, FactoryInfoDTO> deptMap = factoryInfoApi.getFactoryMap(deptIds);

            result.getList().forEach(item -> {
                // 设置借支用户名称
                item.setUserName(userMap.get(item.getUserId()).getNickName());
                // 设置借支用户部门名称
                item.setDeptName(deptMap.get(item.getDeptId()).getName());
            });
        }

        return success(result);
    }
}