package com.zcloud.basic.info.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.config.client.dicttree.facade.ConfDictTreeFacade;
import com.jjb.saas.config.client.dicttree.response.ConfDictTreeCO;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.jjb.saas.system.client.tenant.facade.TenantFacade;
import com.jjb.saas.system.client.tenanttype.facade.TenantTypeFacade;
import com.jjb.saas.system.client.tenanttype.response.TenantTypeCO;
import com.jjb.saas.system.client.unit.facade.UnitFacade;
import com.jjb.saas.system.client.unit.request.OpeUnitAddCmd;
import com.jjb.saas.system.client.unit.request.OpeUnitUpdateCmd;
import com.jjb.saas.system.client.user.facade.UserFacade;
import com.jjb.saas.system.client.user.request.UserUpdateCmd;
import com.zcloud.basic.info.domain.enums.CorpTypeEnum;
import com.zcloud.basic.info.domain.enums.MenuEnum;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.mapper.CorpInfoMapper;
import com.zcloud.basic.info.persistence.repository.CorpInfoRepository;
import com.zcloud.basic.info.utils.Query;
import com.zcloud.gbscommon.utils.MD5;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:17
 */
@Service
@RequiredArgsConstructor
public class CorpInfoRepositoryImpl extends BaseRepositoryImpl<CorpInfoMapper, CorpInfoDO> implements CorpInfoRepository {
    private final CorpInfoMapper corpInfoMapper;

    @DubboReference
    private UnitFacade unitFacade;
    @DubboReference
    private UserFacade userFacade;

    @DubboReference
    private TenantTypeFacade tenantTypeFacade;

    @DubboReference
    private ConfDictTreeFacade confDictTreeFacade;

    @Override
    public PageResponse<CorpInfoDO> listPage(Map<String, Object> params) {
        IPage<CorpInfoDO> iPage = new Query<CorpInfoDO>().getPage(params);
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        List<Integer> enterpriseType = CorpTypeEnum.getCodesByEnterpriseType((Integer) params.get("enterpriseType"));
        if(CollUtil.isNotEmpty(enterpriseType)){
            queryWrapper.in("type",enterpriseType );
        }
        queryWrapper.orderByAsc("corp_order").orderByDesc("create_time");
        String menuPerms = "";
        if (!ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        queryWrapper.eq("delete_enum", "FALSE");
        IPage<CorpInfoDO> result = corpInfoMapper.selectUserPage(iPage, queryWrapper, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public Long getCorpCountByCorpName(String corpName) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corp_name", corpName);
        return corpInfoMapper.selectCount(queryWrapper);
    }

    @Override
    public void saveCorp(CorpInfoDO corpInfoDO) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        Long tenantId = ssoUser.getTenantId();
        MultiResponse<TenantTypeCO> tenantTypeResponse = tenantTypeFacade.listByTenantIdAndNotDefualt(tenantId);
        Map<String, Long> tenantTypeMap = tenantTypeResponse.getData().stream().collect(Collectors.toMap(TenantTypeCO::getTenantTypeName, TenantTypeCO::getId));
        OpeUnitAddCmd ouac = new OpeUnitAddCmd();
        ouac.setTenantName(corpInfoDO.getCorpName());
        ouac.setTenantTypeId(tenantTypeMap.get(CorpTypeEnum.getNameByCode(corpInfoDO.getType())));
        //企业类型(0-普通企业，1-集团单位，2-股份单位，3-相关方企业，4-货主单位，5-驻港单位)
//        if(corpInfoDO.getType() == 0 || corpInfoDO.getType() == 1 || corpInfoDO.getType() == 2){
//            ouac.setTenantTypeId(1990693345743208448L);
//        } else if (corpInfoDO.getType() == 2){
//            ouac.setTenantTypeId(1990693388072124416L);
//        } else if (corpInfoDO.getType() == 3 || corpInfoDO.getType() == 4 || corpInfoDO.getType() == 5){
//            ouac.setTenantTypeId(1990693388072124416L);
//        }
        // LocalDate
        ouac.setEffectiveDate(LocalDate.of(2099, 12, 31));
        ouac.setAccount(corpInfoDO.getCorpName());
        ouac.setTenantId(tenantId);
        ouac.setPassword(corpInfoDO.getPassword());
        try {
            SingleResponse<Long> corpId = unitFacade.add(ouac);
            corpInfoDO.setId(corpId.getData());
        } catch (Exception e){
            e.printStackTrace();
        }

        save(corpInfoDO);

    }

    @Override
    public void updateCorpName(CorpInfoDO corpInfo) {
        UpdateWrapper<CorpInfoDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", corpInfo.getId());
        updateWrapper.set("corp_name", corpInfo.getCorpName());
        update(updateWrapper);


        OpeUnitUpdateCmd ouac = new OpeUnitUpdateCmd();
        ouac.setId(corpInfo.getId());
        ouac.setTenantName(corpInfo.getCorpName());
        ouac.setAccount(corpInfo.getCorpName());
        ouac.setTenantTypeId(1989262038388776960L);
        unitFacade.update(ouac);
    }

    @Override
    public Map<Long, String> getCorpinfoNameByCorpinfoId(List<Long> collect) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", collect);
        List<CorpInfoDO> corpInfoDOList = corpInfoMapper.selectList(queryWrapper);
        Map<Long, String> map = corpInfoDOList.stream().collect(
                Collectors.toMap(CorpInfoDO::getId, CorpInfoDO::getCorpName)
        );

        return map;
    }

    @Override
    public List<CorpInfoDO> getCorpinfoByCode(String code) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        return list(queryWrapper);
    }

    @Override
    public List<CorpInfoDO> getCorpinfoByCodeAndId(String code, Long id) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", id);
        queryWrapper.eq("code", code);
        return list(queryWrapper);
    }

    @Override
    public Long checkCorpName(Map<String, Object> parmas) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corp_name", parmas.get("corpName"));
        if(!ObjectUtil.isEmpty(parmas.get("id"))){
            queryWrapper.ne("id", parmas.get("id"));
        }

        return corpInfoMapper.selectCount(queryWrapper);
    }

    @Override
    public PageResponse<CorpInfoDO> corpUserMiddlePage(Map<String, Object> params) {
        IPage<CorpInfoDO> iPage = new Query<CorpInfoDO>().getPage(params);
        if(ObjectUtil.isNotEmpty(params.get("eqType"))){
            params.put("type", Arrays.asList(params.get("eqType")));
        }else{
            if(ObjectUtil.isNotEmpty(params.get("enterpriseType"))){
                List<Integer> enterpriseType = CorpTypeEnum.getCodesByEnterpriseType((Integer) params.get("enterpriseType"));
                if(CollUtil.isNotEmpty(enterpriseType)){
                    params.put("type", enterpriseType);
                }
            }
        }
        String menuPerms = "";
        if (!ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<CorpInfoDO> result = corpInfoMapper.corpUserMiddlePage(iPage,params, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public CorpInfoDO getCorpInfoByCorpName(String corpName) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corp_name", corpName);
        return corpInfoMapper.selectOne(queryWrapper);
    }
}

