package com.hzs.system.sys.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.exception.base.BaseException;
import com.hzs.common.domain.system.sys.ext.SysApprovalExt;
import com.hzs.common.domain.system.sys.ext.SysApprovalSendExt;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.*;
import com.hzs.system.sys.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description: 审批dubbo服务
 * Author: jiang chao
 * Time: 2022/10/9 11:24
 * Classname: ApprovalServiceProvider
 * PackageName: com.hzs.system.sys.provider
 */
@Slf4j
@DubboService
public class ApprovalServiceProvider implements IApprovalServiceApi {

    @Autowired
    private IApprovalService iApprovalService;
    @Autowired
    private ISysApprovalBusinessMemberService iSysApprovalBusinessMemberService;
    @Autowired
    private ISysApprovalService iSysApprovalService;
    @Autowired
    private ISysApprovalSendService iSysApprovalSendService;


    @Override
    public R<Map<Integer, ApprovalInfoDTO>> queryApprovalInfo(List<Integer> approvalBusinessList, Integer pkCountry) {
        Map<Integer, ApprovalInfoDTO> map = new HashMap<>();
        List<SysApprovalExt> sysApprovalExtList = iSysApprovalService.lastApprovalAndItem(approvalBusinessList, pkCountry);
        if (CollectionUtil.isNotEmpty(sysApprovalExtList)) {
            // 查询抄送人map
            Map<Integer, List<SysApprovalSendExt>> sendMap = iSysApprovalSendService.queryApprovalSendMap(sysApprovalExtList.stream().map(SysApprovalExt::getPkId).collect(Collectors.toList()));

            for (SysApprovalExt sysApprovalExt : sysApprovalExtList) {
                ApprovalInfoDTO approvalInfoDTO = new ApprovalInfoDTO();
                approvalInfoDTO.setEApprovalBusiness(EApprovalBusiness.getEnumByValue(sysApprovalExt.getApprovalBusiness()));
                approvalInfoDTO.setItemList(BeanUtil.copyToList(sysApprovalExt.getItemList(), ApprovalItemInfoDTO.class));
                // 处理抄送人
                List<SysApprovalSendExt> sendList = sendMap.get(sysApprovalExt.getPkId());
                if (CollectionUtil.isNotEmpty(sendList)) {
                    approvalInfoDTO.setSendList(BeanUtil.copyToList(sendList, ApprovalSendInfoDTO.class));
                }
                map.put(sysApprovalExt.getApprovalBusiness(), approvalInfoDTO);
            }
        }
        return R.ok(map);
    }

    @Override
    public R<String> submit(ApprovalSubmitDTO approvalSubmitDTO, LoginUser loginUser) {
        try {
            String str = iApprovalService.submit(approvalSubmitDTO, loginUser, null, null);
            if (null == str) {
                return R.ok();
            }
            return R.fail(str);
        } catch (BaseException e) {
            log.error("提交签呈异常. dto: {}", approvalSubmitDTO, e);
            return R.fail(e.getDefaultMessage());
        } catch (Exception e) {
            log.error("提交签呈异常. dto: {}", approvalSubmitDTO, e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<String> submitBatch(List<ApprovalSubmitDTO> approvalSubmitDTOList, LoginUser loginUser) {
        try {
            String str = iApprovalService.submitBatch(approvalSubmitDTOList, loginUser);
            if (null == str) {
                return R.ok();
            }
            return R.fail(str);
        } catch (BaseException e) {
            log.error("提交签呈异常. listDto: {}", approvalSubmitDTOList, e);
            return R.fail(e.getDefaultMessage());
        } catch (Exception e) {
            log.error("提交签呈异常. listDto: {}", approvalSubmitDTOList, e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<String> submitOrder(ApprovalSubmitDTO approvalSubmitDTO, LoginUser loginUser, Object obj) {
        try {
            String str = iApprovalService.submit(approvalSubmitDTO, loginUser, obj, null);
            if (null == str) {
                return R.ok();
            }
            return R.fail(str);
        } catch (BaseException e) {
            log.error("提交签呈异常. dto: {}", approvalSubmitDTO, e);
            return R.fail(e.getDefaultMessage());
        } catch (Exception e) {
            log.error("提交签呈异常. dto: {}", approvalSubmitDTO, e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<Map<Long, ApprovalMemberNumDTO>> queryProcessingApproval(List<Long> pkMemberList, Integer pkCountry) {
        if (CollectionUtil.isEmpty(pkMemberList)) {
            return R.fail();
        }
        // 返回数据
        Map<Long, ApprovalMemberNumDTO> resultMap = new HashMap<>();

        List<ApprovalMemberNumDTO> memberNumList = iSysApprovalBusinessMemberService.queryProcessingApproval(pkMemberList, pkCountry);
        if (CollectionUtil.isNotEmpty(memberNumList)) {
            for (ApprovalMemberNumDTO approvalMemberNumDTO : memberNumList) {
                resultMap.put(approvalMemberNumDTO.getPkMember(), approvalMemberNumDTO);
            }
        }

        return R.ok(resultMap);
    }

}
