package com.sckw.manage.service;

import java.io.IOException;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.CooperateStatusEnum;
import com.sckw.core.model.enums.SystemTypeEnum;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.excel.utils.EasyExcelUtil;
import com.sckw.manage.dao.KwmCooperateMapper;
import com.sckw.manage.dao.KwmCooperateTypeMapper;
import com.sckw.manage.model.dto.req.CooperateManageQueryReqDto;
import com.sckw.manage.model.entity.KwmCooperate;
import com.sckw.manage.model.entity.KwmCooperateType;
import com.sckw.manage.model.vo.req.BindManagerReqVo;
import com.sckw.manage.model.vo.req.CooperateManageQueryReqVo;
import com.sckw.manage.model.vo.req.FindEntCooperateReqVo;
import com.sckw.manage.model.vo.res.CooperateManageQueryResVo;
import com.sckw.manage.model.vo.res.FindEntCooperateResVo;
import com.sckw.manage.model.vo.res.FindListGroupResVo;
import com.sckw.manage.model.vo.res.QueryDetailResVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author czh
 * @desc 合作管理service
 * @date 2023/7/10
 */
@Slf4j
@Service
public class KwmCooperateManageService {

    @Autowired
    private KwmCooperateMapper kwmCooperateMapper;

    @Autowired
    private KwmCooperateTypeMapper kwmCooperateTypeMapper;

    @Autowired
    private CommonBusinessService commonBusinessService;

    @DubboReference(version = "2.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;


    /**
     * @param reqVo 分页入参
     * @return PageResult
     * @desc: 查询合作企业列表
     * @author: czh
     * @date: 2023/7/10
     */
    public PageResult queryByPage(CooperateManageQueryReqVo reqVo) {
        PageHelper.startPage(reqVo.getPage(), reqVo.getPageSize());
        List<CooperateManageQueryResVo> list = commonBusinessService.findList(buildQueryParam(reqVo));
        return PageHelperUtil.getPageResult(new PageInfo<>(list));
    }


    /**
     * @param reqVo 查询入参
     * @return CooperateManageQueryReqDto
     * @desc: 构建查全量入参
     * @author: czh
     * @date: 2023/7/11
     */
    private CooperateManageQueryReqDto buildQueryParam(CooperateManageQueryReqVo reqVo) {
        CooperateManageQueryReqDto cooperateManageQueryReqDto = new CooperateManageQueryReqDto();
        BeanUtils.copyProperties(reqVo, cooperateManageQueryReqDto);
        String cooperateTypes = reqVo.getCooperateTypes();
        if (StringUtils.isNotBlank(cooperateTypes)) {
            cooperateManageQueryReqDto.setCooperateTypes(Arrays.asList(cooperateTypes.split(Global.COMMA)));
        }


        //只允许查出申请中、已拒绝、已撤销的记录
        List<Integer> status = new ArrayList<>();
        if (Objects.isNull(reqVo.getStatus())) {
            status.add(CooperateStatusEnum.PROCESS.getCode());
            status.add(CooperateStatusEnum.OK.getCode());
            status.add(CooperateStatusEnum.PASS.getCode());
        }

        if (Objects.nonNull(reqVo.getStatus()) && reqVo.getStatus().equals(CooperateStatusEnum.PROCESS.getCode())) {
            status.add(CooperateStatusEnum.PROCESS.getCode());
        }
        if (Objects.nonNull(reqVo.getStatus()) && reqVo.getStatus().equals(CooperateStatusEnum.OK.getCode())) {
            status.add(CooperateStatusEnum.OK.getCode());
        }
        if (Objects.nonNull(reqVo.getStatus()) && reqVo.getStatus().equals(CooperateStatusEnum.PASS.getCode())) {
            status.add(CooperateStatusEnum.PASS.getCode());
        }
        cooperateManageQueryReqDto.setStatus(status);


        List<Long> ids = reqVo.getIds();
        if (!CollectionUtils.isEmpty(ids)) {
            cooperateManageQueryReqDto.setIds(ids);
        }
        return cooperateManageQueryReqDto;
    }


    /**
     * @param entId 企业id
     * @return QueryDetailResVo
     * @desc: 查询详情
     * @author: czh
     * @date: 2023/7/10
     */
    public QueryDetailResVo queryDetail(Long entId) {
        EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(entId);
        if (Objects.isNull(entCacheResDto)) {
            return null;
        }
        QueryDetailResVo queryDetailResVo = new QueryDetailResVo();
        BeanUtils.copyProperties(entCacheResDto, queryDetailResVo);
        return queryDetailResVo;
    }

    /**
     * @param reqVo 用户id
     * @desc: 设置客户经理
     * @author: czh
     * @date: 2023/7/10
     */
    public void bindManager(BindManagerReqVo reqVo) throws SystemException {
       commonBusinessService.bingManager(reqVo.getUserId(), reqVo.getId());
    }

    /**
     * @param ids 主键id
     * @desc: 解除合同
     * @author: czh
     * @date: 2023/7/11
     */
    @Transactional(rollbackFor = {})
    public void dissolve(String ids) {
        //todo 判断是否可以解除（若和所选企业有未完结的业务单据（销售订单/采购订单、托运订单/承运订单、对账单、结算单），不支持解除合作）
        //提示：您与所选企业存在未完结业务，当前无法删除！
        List<KwmCooperate> kwmCooperates = kwmCooperateMapper.selectBatchIds(Arrays.asList(ids.split(Global.COMMA)));
        if (CollectionUtils.isEmpty(kwmCooperates)) {
            throw new SystemException(HttpStatus.QUERY_FAIL_CODE, HttpStatus.COOPERATE_NOT_EXISTS);
        }
        Long userId = LoginUserHolder.getUserId();
        Date date = new Date();
        kwmCooperates.forEach(item -> {
            //运营端可以操作任意状态，企业端只能操作合作中的数据
            if (checkUserPermission(item.getStatus())) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.MSG_012);
            }

            item.setStatus(CooperateStatusEnum.PASS.getCode());
            item.setUpdateBy(userId);
            item.setUpdateTime(date);
            if (kwmCooperateMapper.updateById(item) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
            }
        });

    }

    /**
     * @param status 操作状态
     * @return boolean
     * @desc: 校验用户操作状态
     * @author: czh
     * @date: 2023/7/11
     */
    private boolean checkUserPermission(int status) {
        return status != CooperateStatusEnum.OK.getCode() && LoginUserHolder.getSystemType() != SystemTypeEnum.MANAGE.getCode();
    }

    /**
     * @param ids 主键
     * @desc: 删除合同
     * @author: czh
     * @date: 2023/7/11
     */
    public void delete(String ids) {
        List<KwmCooperate> kwmCooperates = kwmCooperateMapper.selectBatchIds(Arrays.asList(ids.split(Global.COMMA)));
        Long userId = LoginUserHolder.getUserId();
        Date date = new Date();
        kwmCooperates.forEach(item -> {
            //运营端可以操作任意状态，企业端只能操作合作中的数据
            if (item.getStatus() != CooperateStatusEnum.PASS.getCode()) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.MSG_013);
            }

            item.setDelFlag(Global.YES);
            item.setUpdateBy(userId);
            item.setUpdateTime(date);
            if (kwmCooperateMapper.updateById(item) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
            }
        });
    }

    /**
     * @param reqVo 查询入参
     * @desc: 导出
     * @author: czh
     * @date: 2023/7/11
     */
    public void export(CooperateManageQueryReqVo reqVo) throws SystemException {
        List<CooperateManageQueryResVo> list = commonBusinessService.findList(buildQueryParam(reqVo));
        try {
            EasyExcelUtil.writeSingleExcel("合作管理.xlsx", "sheet1", list, CooperateManageQueryResVo.class);
        } catch (IOException e) {
            log.error("导出失败:", e);
            throw new SystemException(HttpStatus.CODE_10301, HttpStatus.MSG_014);
        }
    }

    /**
     * @param reqVo 查询入参
     * @return HttpResult
     * @desc: 分组查询
     * @author: czh
     * @date: 2023/7/17
     */
    public List<FindListGroupResVo> findListGroup(CooperateManageQueryReqVo reqVo) {
        //初始化分组返参
        List<FindListGroupResVo> result = commonBusinessService.init();

        List<CooperateManageQueryResVo> list = commonBusinessService.findList(buildQueryParam(reqVo));
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }

        //填充分组数据
        commonBusinessService.fillGroup(result, list);
        return result;
    }


    /**
     * @param reqVo 企业id
     * @return FindEntCooperateResVo
     * @desc: 查询企业的合作单位
     * @author: czh
     * @date: 2023/7/22
     */
    public List<FindEntCooperateResVo> findEntCooperate(FindEntCooperateReqVo reqVo) {
        Long id = reqVo.getId();
        Integer cooperateType = reqVo.getCooperateType();
        LambdaQueryWrapper<KwmCooperate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwmCooperate::getInviterEntId, id).or().
                eq(KwmCooperate::getInviteeEntId, id).and(
                        wp -> wp.eq(KwmCooperate::getDelFlag, Global.NO).
                                 eq(KwmCooperate::getStatus, CooperateStatusEnum.OK.getCode())
                );
        List<KwmCooperate> kwmCooperates = kwmCooperateMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(kwmCooperates)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<KwmCooperateType> kwmCooperateTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        kwmCooperateTypeLambdaQueryWrapper.in(KwmCooperateType::getCooperateId, kwmCooperates.stream().map(KwmCooperate::getId).toList());
        kwmCooperateTypeLambdaQueryWrapper.eq(KwmCooperateType::getDelFlag, Global.NO);
        kwmCooperateTypeLambdaQueryWrapper.eq(KwmCooperateType::getType, cooperateType);
        List<KwmCooperateType> kwmCooperateTypes = kwmCooperateTypeMapper.selectList(kwmCooperateTypeLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(kwmCooperateTypes)) {
            return Collections.emptyList();
        }

        //装对方企业的id
        List<Long> kwmCooperateIdList = kwmCooperateTypes.stream().map(KwmCooperateType::getCooperateId).toList();
        List<Long> targetEntIdList = new ArrayList<>();
        kwmCooperates.forEach(item -> {
            if (kwmCooperateIdList.contains(item.getId())) {
                targetEntIdList.add(item.getInviterEntId().equals(id) ? item.getInviteeEntId() : item.getInviterEntId());
            }
        });

        List<EntCacheResDto> entCacheResDtos = remoteSystemService.queryEntCacheByIds(targetEntIdList);
        if (CollectionUtils.isEmpty(entCacheResDtos)) {
            return Collections.emptyList();
        }

        //装对方企业信息
        List<FindEntCooperateResVo> list = new ArrayList<>();
        entCacheResDtos.forEach(item -> {
            FindEntCooperateResVo findEntCooperateResVo = new FindEntCooperateResVo();
            findEntCooperateResVo.setEntId(item.getId());
            findEntCooperateResVo.setEntName(item.getFirmName());
            list.add(findEntCooperateResVo);
        });

        //装对方企业的主体单位
        Map<Long, List<EntCacheResDto>> longListMap = remoteSystemService.queryEntDeptCacheByIds(targetEntIdList);
        List<Long> longs = list.stream().map(FindEntCooperateResVo::getEntId).toList();
        longs.forEach(item -> {
            List<EntCacheResDto> entCacheResDtos1 = longListMap.get(item);
            if (CollectionUtils.isEmpty(entCacheResDtos1)) {
                return;
            }
            entCacheResDtos1.forEach(x -> {
                FindEntCooperateResVo findEntCooperateResVo = new FindEntCooperateResVo();
                findEntCooperateResVo.setEntId(x.getId());
                findEntCooperateResVo.setEntName(x.getFirmName());
                list.add(findEntCooperateResVo);
            });
        });
        return list;
    }

}
