package com.retdata.yaoyibi.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retdata.yaoyibi.domain.*;
import com.retdata.yaoyibi.mapper.CsoSysUserMapper;
import com.retdata.yaoyibi.service.CsoSysUserService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional(readOnly = true)
@DataSource(DataSourceType.CSO)
public class CsoSysUserServiceImpl extends ServiceImpl<CsoSysUserMapper, CsoSysUser> implements CsoSysUserService {

    private static final Map<String, Map<String, String>> certStatusDict = new HashMap<>();

    static {
        // 根据 gig_type取值进行区分 
        //gig_type=0, "人力家"
        //    1, "认证成功")
        //    2, "认证失败")
        //    3, "认证中"),
        //    4, "未认证");
        //
        //1, "税邦云"
        //    10, "未认证"),         
        //    11, "已增员"),      
        //    12, "已认证未绑卡")
        //    13, "认证中已绑卡"),
        //    14, "认证中未绑卡"
        //    15, "已认证"),            
        //    16, "未认证已绑卡")
        //
        //2, "钉灵工"
        //10, "深圳之光"
        //13, "合创众盈"
        //15, "深圳产业园"
        //    UN_SIGNED("0", "未签约"),
        //	CERT("1", "已认证"),
        //    SIGNED("2", "已签约"),
        //	SIGNING("3", "签约中"),
        //	UN_CERT("4", "未认证");
        //
        //4, "正启之星"
        //11, "天津易捷"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约");
        //
        //5, "浪潮" 
        //17, "浪潮装备"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约")
        //
        //6, "汇企云"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约");
        //
        //7, "众蚁云"
        //9, "福建企赋"
        //16, "全思福"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约"),
        //	SIGNING(3, "签约中");
        //
        //8, "中智"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约");
        //
        //12, "薪事易"
        //14, "海南飞亿"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约"),
        //	UN_CERT(3, "未认证");
        //
        //18, "正启之星"
        //19, "全思福-灵就宫"
        //20, "全思福-上海鹊诊"
        //21, "东进人才"
        //    UN_SIGN(0, "未签约"),
        //	CERT(1, "已认证"),
        //	SIGN(2, "已签约");
        Map<String, String> dict = new HashMap<>();
        dict.put("1", "认证成功");
        dict.put("2", "认证失败");
        dict.put("3", "认证中");
        dict.put("4", "未认证");
        certStatusDict.put("0", dict);
        dict = new HashMap<>();
        dict.put("10", "未认证");
        dict.put("11", "已增员");
        dict.put("12", "已认证未绑卡");
        dict.put("13", "认证中已绑卡");
        dict.put("14", "认证中未绑卡");
        dict.put("15", "已认证");
        dict.put("16", "未认证已绑卡");
        certStatusDict.put("1", dict);
        dict = new HashMap<>();
        dict.put("0", "未签约");
        dict.put("1", "已认证");
        dict.put("2", "已签约");
        dict.put("3", "签约中");
        dict.put("4", "未认证");
        certStatusDict.put("2", dict);
        certStatusDict.put("10", dict);
        certStatusDict.put("13", dict);
        certStatusDict.put("15", dict);
        dict = new HashMap<>();
        dict.put("0", "未签约");
        dict.put("1", "已认证");
        dict.put("2", "已签约");
        certStatusDict.put("4", dict);
        certStatusDict.put("11", dict);
        certStatusDict.put("5", dict);
        certStatusDict.put("17", dict);
        certStatusDict.put("6", dict);
        certStatusDict.put("8", dict);
        certStatusDict.put("18", dict);
        certStatusDict.put("19", dict);
        certStatusDict.put("20", dict);
        certStatusDict.put("21", dict);
        dict = new HashMap<>();
        dict.put("0", "未签约");
        dict.put("1", "已认证");
        dict.put("2", "已签约");
        dict.put("3", "签约中");
        certStatusDict.put("7", dict);
        certStatusDict.put("9", dict);
        certStatusDict.put("16", dict);
        dict.put("0", "未签约");
        dict.put("1", "已认证");
        dict.put("2", "已签约");
        dict.put("3", "未认证");
        certStatusDict.put("12", dict);
        certStatusDict.put("14", dict);
    }

    @Override
    public Page<CsoSysUserSearchDto> search(CsoSysUserSearchParams searchParams, Pageable pageable) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<CsoSysUserSearchDto> page =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageable.getPageNumber() + 1,
                        pageable.getPageSize());
        page.addOrder(OrderItem.desc("u1.create_time"));
        page = getBaseMapper().selectPageByParams(page, searchParams);
        for (CsoSysUserSearchDto dto : page.getRecords()) {
            dto.setSubCertStatus(processSubCertStatus(dto.getSubCertStatus()));
        }
        return new PageImpl<>(page.getRecords(), pageable, page.getTotal());
    }

    @Override
    public CsoSysUserDetailDto getDetailById(Integer userId) {
        CsoSysUserDetailDto detail = getBaseMapper().selectDetailById(userId);
        detail.setSubCertStatus(processSubCertStatus(detail.getSubCertStatus()));
        return detail;
    }

    private String processSubCertStatus(String subCertStatus) {
        if (StringUtils.isEmpty(subCertStatus)) {
            return subCertStatus;
        }
        String[] subCertStatusArr = subCertStatus.split(",");
        StringBuilder sb = new StringBuilder();
        for (String s : subCertStatusArr) {
            int idx = s.indexOf(':');
            String key = s.substring(0, idx);
            String value = s.substring(idx + 1);
            idx = value.indexOf('_');
            String gigType = value.substring(0, idx);
            String certStatus = value.substring(idx + 1);
            Map<String, String> dict = certStatusDict.get(gigType);
            if (dict != null) {
                value = dict.getOrDefault(certStatus, certStatus);
            } else {
                value = certStatus;
            }
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(key).append(":").append(value);
        }
        return sb.toString();
    }

    @Override
    public List<CsoSysDeptSummary> listDeptSummary() {
        return getBaseMapper().selectDeptSummary();
    }

    @Override
    public List<WmDaDrugEnt> listDrugEnt() {
        return getBaseMapper().selectDrugEnt();
    }
}
