package com.goldenwater.slaj.hidd.service.impl;

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

import com.goldenwater.slaj.hidd.domain.ObjHiddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.slaj.hidd.mapper.ObjHiddMapper;
import com.goldenwater.slaj.hidd.domain.ObjHidd;
import com.goldenwater.slaj.hidd.service.IObjHiddService;
import com.goldenwater.slaj.hidd.service.IBisHiddInveService;
import com.goldenwater.slaj.hidd.domain.BisHiddInve;

/**
 * 隐患对象 服务层实现
 */
@Service
public class ObjHiddServiceImpl implements IObjHiddService {

    @Autowired
    private ObjHiddMapper objHiddMapper;

    @Autowired
    private IBisHiddInveService bisHiddInveService;

    @Override
    public ObjHidd selectObjHiddByGuid(String guid) {
        return objHiddMapper.selectObjHiddByGuid(guid);
    }

    @Override
    public List<ObjHiddVo> selectObjHiddList(ObjHidd objHidd) {
        return objHiddMapper.selectObjHiddList(objHidd);
    }

    @Override
    public int insertObjHidd(ObjHidd objHidd) {
        return objHiddMapper.insertObjHidd(objHidd);
    }

    @Override
    public int updateObjHidd(ObjHidd objHidd) {
        return objHiddMapper.updateObjHidd(objHidd);
    }

    @Override
    public int deleteObjHiddByGuid(String guid) {
        return objHiddMapper.deleteObjHiddByGuid(guid);
    }

    @Override
    public int deleteObjHiddByGuids(String[] guids) {
        return objHiddMapper.deleteObjHiddByGuids(guids);
    }

    @Override
    public List<ObjHiddVo> selectObjHiddListByName(ObjHidd objHidd) {
        return objHiddMapper.selectObjHiddListByName(objHidd);
    }

    @Override
    public List<ObjHiddVo> selectNextDepHiddList(ObjHidd objHidd) {
        return objHiddMapper.selectNextDepHiddList(objHidd);
    }

    @Override
    public int insertNextHidd(ObjHidd objHidd) {
        return objHiddMapper.insertNextHidd(objHidd);
    }

    @Override
    public List<Map<String, Object>> selectFastRepTreeData(String parentGuid) {
        List<Map<String, Object>> tree = new ArrayList<>();
        // 构建树形数据
        Map<String, Object> node = new HashMap<>();
        node.put("id", "root");
        node.put("label", "全部");
        node.put("children", new ArrayList<>());
        tree.add(node);
        return tree;
    }

    @Override
    public List<Map<String, Object>> selectHiddGroupBy(Map<String, Object> params) {
        return objHiddMapper.selectHiddGroupBy(params);
    }

    @Override
    @SuppressWarnings("unchecked")
    public int compoundSave(Map<String, Object> params) {
        // 保存隐患基本信息 obj_hidd
        ObjHidd objHidd = new ObjHidd();
        if (params.containsKey("guid")) {
            objHidd.setGuid((String) params.get("guid"));
        }
        objHidd.setHiddName((String) params.get("hiddName"));
        objHidd.setEngGuid((String) params.get("engGuid"));
        objHidd.setHiddGrad((String) params.get("hiddGrad"));
        objHidd.setHiddClas((String) params.get("hiddClas"));
        objHidd.setProPart((String) params.get("proPart"));
        objHidd.setOtlcolQiffFnvu((String) params.get("hiddDesc"));
        objHidd.setHiddStat((String) params.get("hiddStat"));
        objHidd.setHiddSort((String) params.get("hiddSort"));
        objHidd.setOrgGuid((String) params.get("orgGuid"));

        int result;
        if (params.containsKey("guid") && params.get("guid") != null) {
            result = objHiddMapper.updateObjHidd(objHidd);
        } else {
            String guid = java.util.UUID.randomUUID().toString().replace("-", "");
            objHidd.setGuid(guid);
            result = objHiddMapper.insertObjHidd(objHidd);
            objHidd.setGuid(guid);
        }

        // 保存排查信息 bis_hidd_inve
        if (params.containsKey("bisHiddInveDto")) {
            Object inveObj = params.get("bisHiddInveDto");
            if (inveObj instanceof List) {
                List<Map<String, Object>> inveList = (List<Map<String, Object>>) inveObj;
                if (!inveList.isEmpty()) {
                    Map<String, Object> inveData = inveList.get(0);
                    BisHiddInve inve = new BisHiddInve();
                    if (inveData.containsKey("guid")) {
                        inve.setGuid((String) inveData.get("guid"));
                    }
                    inve.setHiddGuid(objHidd.getGuid());
                    inve.setInspDate((String) inveData.get("inspDate"));
                    inve.setInspOrgGuid((String) inveData.get("inspOrgGuid"));
                    inve.setInspLeader((String) inveData.get("recPers"));
                    inve.setInspLeaderPost((String) inveData.get("inspLeaderPost"));
                    if (inve.getGuid() != null) {
                        bisHiddInveService.updateBisHiddInve(inve);
                    } else {
                        String inveGuid = java.util.UUID.randomUUID().toString().replace("-", "");
                        inve.setGuid(inveGuid);
                        bisHiddInveService.insertBisHiddInve(inve);
                    }
                }
            }
        }

        return result;
    }
}
