package com.docus.server.archive.service.impl;

import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.archive.constans.SyncConstant;
import com.docus.server.archive.entity.PowerDept;
import com.docus.server.archive.mapper.PowerDeptMapper;
import com.docus.server.archive.service.FoShanThreeHospDeptInfoSyncService;
import com.docus.server.archive.utils.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YongBin Wen
 * @date 2024/3/27 16:25
 */
@Service("fssy" + SyncConstant.DEPT_INFO_SYNC_SERVICE_SUFFIX)
@Slf4j
public class FoShanThreeHospDeptInfoSyncServiceImpl implements FoShanThreeHospDeptInfoSyncService {
    @Resource
    private PowerDeptMapper powerDeptMapper;
    @Resource
    private IdService idService;

    @Override
    public void syncByUnifyMessageReceive(String message) {
        List<PowerDept> powerDeptList=parsePowerDept(message);
        if(Func.isNotEmpty(powerDeptList)){
            saveOrUpdate(powerDeptList);
        }
    }


    private  List<PowerDept> parsePowerDept(String message) {
        ArrayList<PowerDept> depts = new ArrayList<>();

        XmlUtil xmlUtil = XmlUtil.of(message);
        Node deptCodeNode = xmlUtil.getNode("/Request/Body/ResultContent/Dept/DEPT_CODE");
        Node deptNameNode = xmlUtil.getNode("/Request/Body/ResultContent/Dept/DEPT_NAME");
//        Node deptStopNode = xmlUtil.getNode("/Request/Body/ResultContent/Dept/IS_STOP");
        String deptCode = deptCodeNode.getTextContent();
        String deptName = deptNameNode.getTextContent();
        if(Func.isBlank(deptCode)){
            throw new BaseException("科室编码不能为空！");
        }
        if(Func.isBlank(deptName)){
            throw new BaseException("科室名称不能为空！");
        }

        PowerDept dept = new PowerDept();
        dept.setDeptCode(deptCode);
        dept.setDeptName(deptName);
        dept.setEffective(1);

        depts.add(dept);
        return depts;
    }

    /**
     * 根据修改的科室信息，根据科室code判断是否存在，如果存在则更新科室名称状态信息，如果不存在则插入科室信息
     *
     * @param iuPowerDeptList 修改的科室信息
     */
    private void saveOrUpdate(List<PowerDept> iuPowerDeptList) {
        Date nowDate = new Date();
        List<String> iuDeptCodes = iuPowerDeptList.stream()
                .map(PowerDept::getDeptCode)
                .collect(Collectors.toList());
        List<PowerDept> existsPowerDeptList = powerDeptMapper.findByDeptCodes(iuDeptCodes);
        Map<String, PowerDept> existsPowerDeptMap = existsPowerDeptList.stream().collect(Collectors.toMap(PowerDept::getDeptCode, Function.identity()));


        List<PowerDept> insertDeptList = new ArrayList<>();
        List<PowerDept> updateDeptList = new ArrayList<>();
        for (PowerDept iuPowerDept : iuPowerDeptList) {
            if (existsPowerDeptMap.containsKey(iuPowerDept.getDeptCode())) {
                PowerDept powerDept = existsPowerDeptMap.get(iuPowerDept.getDeptCode());
                powerDept.setDeptName(iuPowerDept.getDeptName());
                powerDept.setEffective(iuPowerDept.getEffective());
                powerDept.setUpdateDate(nowDate);
                powerDept.setUpdater("java-sync");
                updateDeptList.add(powerDept);
                continue;
            }
            iuPowerDept.setDeptId(idService.getDateSeq());
            iuPowerDept.setUpdater("java-sync");
            iuPowerDept.setUpdateDate(nowDate);
            iuPowerDept.setCreater("java-sync");
            iuPowerDept.setCreateDate(nowDate);
            iuPowerDept.setType(1);
            iuPowerDept.setSort(0);
            insertDeptList.add(iuPowerDept);
        }
        if (Func.isNotEmpty(insertDeptList)) {
            powerDeptMapper.insertBatch(insertDeptList);
        }

        if (Func.isNotEmpty(updateDeptList)) {
            powerDeptMapper.updateBatch(updateDeptList);
        }

    }
}
