package org.springblade.system.convert;

import org.mapstruct.*;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.constant.DataStatusEnum;
import org.springblade.system.pojo.entity.Dept;
import org.springblade.system.pojo.enums.DeptCategory;
import org.springblade.system.pojo.vo.DeptListVO;
import org.springblade.thirdparty.mk.constant.MKConstant;
import org.springblade.thirdparty.mk.pojo.dto.MKPushOrgDeptDTO;
import org.springblade.thirdparty.oa.constant.OAConstant;
import org.springblade.thirdparty.oa.constant.OAConvertConstant;
import org.springblade.thirdparty.oa.pojo.response.OACompanyResponse;
import org.springblade.thirdparty.oa.pojo.response.OADepartmentResponse;
import org.springblade.thirdparty.oa.util.OAUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  oa公司、部门到本系统部门转换工具
 * @author bfhuange
 * @date 2024/8/26
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface DeptConvert {

    DeptListVO entity2vo(Dept entity);

    List<DeptListVO> entities2vos(List<Dept> entities);

    @Mapping(source = "deptCode", target = "fdId")
    @Mapping(source = "deptName", target = "fdName")
    @Mapping(source = "deptCode", target = "fdNo")
    @Mapping(source = "sort", target = "fdOrder")
    @Mapping(source = "status", target = "fdIsAvailable", qualifiedByName = "fdIsAvailable")
    @Mapping(source = "deptCategory", target = "fdOrgType", qualifiedByName = "fdOrgType")
    @Mapping(source = "deptCategory", target = "fdSourceSystem", qualifiedByName = "fdSourceSystem")
    @Mapping(source = "parentCode", target = "fdParent")
    MKPushOrgDeptDTO entity2dto(Dept entity);

    List<MKPushOrgDeptDTO> entities2dtos(List<Dept> entities);

    @Mapping(source = "subcompanyname", target = "deptName")
    @Mapping(source = "subcompanydesc", target = "fullName")
    // 父公司id转为父部门编码，要拼接前缀
    @Mapping(source = "supsubcomid", target = "parentCode")
    @Mapping(source = "created", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "modified", target = "updateTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    Dept baseConvert(OACompanyResponse company);

    @Mapping(source = "departmentname", target = "deptName")
    @Mapping(source = "departmentmark", target = "fullName")
    // 父部门id转为父部门编码，要拼接前缀
    @Mapping(source = "supdepid", target = "parentCode")
    // 所属子公司id转为所属公司编码，要拼接前缀
    @Mapping(source = "subcompanyid1", target = "belongCompanyCode")
    @Mapping(source = "created", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "modified", target = "updateTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    Dept baseConvert(OADepartmentResponse department);

    /**
     *  oa公司转本系统部门
     * @param company
     * @return
     */
    default Dept company2dept(OACompanyResponse company) {
        Dept dept = this.baseConvert(company);
        // 部门编码添加前缀
        dept.setDeptCode(OAConvertConstant.COMPANY_OA_PREFIX + company.getId());
        String parentCode = dept.getParentCode();
        if (!OAConstant.ROOT_COMPANY_ID.equals(parentCode)) {
            // 父部门编码不是0，拼接前缀
            dept.setParentCode(OAConvertConstant.COMPANY_OA_PREFIX + parentCode);
            // 父部门id后续处理
        } else {
            // 父部门编码是0，父id也设置为0
            dept.setParentId(OAConvertConstant.ROOT_PARENT_ID);
        }
        // 公司所属公司编码就是他自己的部门编码
        dept.setBelongCompanyCode(dept.getDeptCode());
        // 显示顺序处理
        dept.setSort(OAUtils.parseInt(company.getShoworder()));
        // 部门类型，公司
        dept.setDeptCategory(DeptCategory.COMPANY.getCode());
        dept.setSyncTime(new Date());
        // 是oa的
        dept.setIsOa(CommonConstant.YES);
        return dept;
    }

    /**
     *  oa部门转本系统部门
     * @param oaDept
     * @param companyDeptMap 所有公司的部门编码与id map
     * @return
     */
    default Dept dept2dept(OADepartmentResponse oaDept, Map<String, Long> companyDeptMap) {
        Dept dept = this.baseConvert(oaDept);
        // 部门编码添加前缀
        dept.setDeptCode(OAConvertConstant.DEPARTMENT_OA_PREFIX + oaDept.getId());
        // 部门所属公司编码就是他所属公司编码加前缀
        dept.setBelongCompanyCode(OAConvertConstant.COMPANY_OA_PREFIX + dept.getBelongCompanyCode());
        String parentCode = dept.getParentCode();
        if (!OAConstant.ROOT_COMPANY_ID.equals(parentCode)) {
            // 父部门编码不是0，拼接前缀
            dept.setParentCode(OAConvertConstant.DEPARTMENT_OA_PREFIX + parentCode);
            // 父部门id后续处理
        } else {
            // 父部门编码是0，设置为公司编码
            dept.setParentCode(dept.getBelongCompanyCode());
            // 根据父部门编码获取对应的id
            dept.setParentId(companyDeptMap.get(dept.getParentCode()));
        }

        // 显示顺序处理
        dept.setSort(OAUtils.parseInt(oaDept.getShoworder()));
        // 部门类型，部门
        dept.setDeptCategory(DeptCategory.DEPT.getCode());
        dept.setSyncTime(new Date());
        // 是oa的
        dept.setIsOa(CommonConstant.YES);
        return dept;
    }

    @Named("fdIsAvailable")
    default Boolean fdIsAvailable(Integer status) {
        return DataStatusEnum.ENABLE.getCode().equals(status);
    }

    @Named("fdOrgType")
    default String fdOrgType(Integer deptCategory) {
        return DeptCategory.COMPANY.getCode().equals(deptCategory) ? MKConstant.ORG_TYPE_ORG : MKConstant.ORG_TYPE_DEPT;
    }

    @Named("fdSourceSystem")
    default String fdSourceSystem(Integer deptCategory) {
        return CommonConstant.SYSTEM_CODE;
    }

}
