package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.salary.BpmOASalaryCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.salary.BpmOASalaryRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASalaryDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOASalaryService;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.FactoryInfoApi;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

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

/**
 * OA 薪资付款申请 Controller
 *
 * @author 符溶馨

 */
@Tag(name = "管理后台 - OA 薪资付款申请")
@RestController
@RequestMapping("/bpm/oa/salary")
@Validated
public class BpmOASalaryController {

    @Resource
    private BpmOASalaryService salaryService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private FactoryInfoApi factoryInfoApi;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createSalary(@Valid @RequestBody BpmOASalaryCreateReqVO createReqVO) {

//        if (createReqVO.getFactoryId() != null) {
//            DeptRespDTO dto = deptApi.getDeptByFactoryId(createReqVO.getFactoryId()).getCheckedData();
//            createReqVO.setCompanyDeptId(dto.getId());
//        }
        return success(salaryService.createSalary(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得薪资付款申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOASalaryRespVO> getSalary(@RequestParam("id") Long id) {

        BpmOASalaryDO salary = salaryService.getSalary(id);
        if (salary == null) {
            return success(new BpmOASalaryRespVO());
        }

        BpmOASalaryRespVO respVO = BeanUtils.toBean(salary, BpmOASalaryRespVO.class);
        if (salary.getCompanyDeptId() != null) {
            // 获取部门详情
            DeptRespDTO dto = getDept(salary.getCompanyDeptId());

            respVO.setCompanyName(dto.getName());
        }else {
            // 获取工厂信息
            Set<Long> factoryIds = salary.getFactoryDeptId();
            List<FactoryInfoDTO> factoryInfoDTOS = factoryInfoApi.getFactoryInfoList(factoryIds).getCheckedData();

            String factoryNames = factoryInfoDTOS.stream()
                    .map(FactoryInfoDTO::getShortName)
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(","));

            respVO.setCompanyName(factoryNames);
        }

        return success(respVO);
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得薪资付款申请")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOASalaryRespVO> getByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOASalaryDO salary = salaryService.getByProcessInstanceId(processInstanceId);

        if (salary == null) {
            return success(new BpmOASalaryRespVO());
        }

        BpmOASalaryRespVO respVO = BeanUtils.toBean(salary, BpmOASalaryRespVO.class);
        if (salary.getCompanyDeptId() != null) {
            // 获取部门详情
            DeptRespDTO dto = getDept(salary.getCompanyDeptId());

            respVO.setCompanyName(dto.getName());
        }else {
            // 获取工厂信息
            Set<Long> factoryIds = salary.getFactoryDeptId();
            List<FactoryInfoDTO> factoryInfoDTOS = factoryInfoApi.getFactoryInfoList(factoryIds).getCheckedData();

            String factoryNames = factoryInfoDTOS.stream()
                    .map(FactoryInfoDTO::getShortName)
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(","));

            respVO.setCompanyName(factoryNames);
        }

        return success(respVO);
    }

    /**
     * 获得部门信息
     * @param deptId 部门id
     * @return 部门信息
     */
    public DeptRespDTO getDept(Long deptId) {

        if (deptId == null) {
            return new DeptRespDTO();
        }
        return deptApi.getDept(deptId).getCheckedData();
    }
}
