package com.srs.comprehensive.service.impl;

import com.srs.common.core.domain.entity.SysDept;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.Gxsdxy01Zzjg;
import com.srs.comprehensive.mapper.Gxsdxy01ZzjgMapper;
import com.srs.comprehensive.service.Gxsdxy01ZzjgService;
import com.srs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.unit.DataUnit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;


/**
 * (Gxsdxy01Zzjg)表服务实现类
 *
 * @author Chao
 * @since 2023-07-26 17:15:42
 */
@Service("gxsdxy01ZzjgService")
public class Gxsdxy01ZzjgServiceImpl implements Gxsdxy01ZzjgService {
    @Resource
    private Gxsdxy01ZzjgMapper gxsdxy01ZzjgMapper;

    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */

    @Override
    public List<Gxsdxy01Zzjg> queryAll(Gxsdxy01Zzjg gxsdxy01Zzjg) {
        return this.gxsdxy01ZzjgMapper.queryAll(gxsdxy01Zzjg);
    }

    /**
     * 修改数据
     *
     * @param gxsdxy01Zzjg 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Gxsdxy01Zzjg gxsdxy01Zzjg) {

        return  this.gxsdxy01ZzjgMapper.update(gxsdxy01Zzjg);
    }
    /**
     * 从中间表同步数据
     */
    @Override
    public Map<String, Integer> dataSync() {
        HashMap<String, Integer> map = new HashMap<>();
        int success =0;
        int error =0;
        Gxsdxy01Zzjg gxsd = new Gxsdxy01Zzjg();

        //第二层
        gxsd.setBmcj(2L);
        List<Gxsdxy01Zzjg> gxsdxy2 = gxsdxy01ZzjgMapper.queryAll(gxsd);
        Map<String, Integer> map2 = insDept(gxsdxy2);
        success += map2.get("success");
        error +=map2.get("error");
        //第三层
        gxsd.setBmcj(3L);
        List<Gxsdxy01Zzjg> gxsdxy3 = gxsdxy01ZzjgMapper.queryAll(gxsd);
        Map<String, Integer> map3 = insDept(gxsdxy3);
        success += map3.get("success");
        error +=map3.get("error");
        map.put("success",success);
        map.put("error",error);
        return map;
    }
    public Map<String, Integer>  insDept(List<Gxsdxy01Zzjg> gxsdxy){
        HashMap<String, Integer> map = new HashMap<>();
        int success =0;
        int error =0;
        if (gxsdxy != null){
            for (int i = 0; i < gxsdxy.size(); i++) {
                try {
                    Gxsdxy01Zzjg zzjg = gxsdxy.get(i);
                    SysDept dept = sysDeptService.selectDeptByDeptCode(zzjg.getBmbh());
                    if (dept!=null){
                        continue;
                    }
                    SysDept sysDept = new SysDept();
                    sysDept.setDeptCode(zzjg.getBmbh());
                    sysDept.setDeptName(zzjg.getBmmc());
                    sysDept.setOrderNum(i);

                    sysDept.setParentId(sysDeptService.selectDeptByDeptCode(zzjg.getBmbhParent()).getDeptId());

                    int num = sysDeptService.insertDept(sysDept);
                    if (num > 0){
                        success++;
                    }
                }catch (Exception e){
                    System.out.println(e);
                    error++;
                }

            }
        }
        map.put("success",success);
        map.put("error",error);
        return map;
    }






}

