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

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.slaj.acci.mapper.ObjAcciMapper;
import com.goldenwater.slaj.acci.mapper.BisAcciInveMapper;
import com.goldenwater.slaj.acci.mapper.BisAcciExpRepMapper;
import com.goldenwater.slaj.acci.domain.ObjAcci;
import com.goldenwater.slaj.acci.domain.ObjAcciVo;
import com.goldenwater.slaj.acci.domain.BisAcciInve;
import com.goldenwater.slaj.acci.domain.BisAcciExpRep;
import com.goldenwater.slaj.acci.service.IObjAcciService;

/**
 * 事故对象 服务层实现
 */
@Service
public class ObjAcciServiceImpl implements IObjAcciService {

    @Autowired
    private ObjAcciMapper objAcciMapper;

    @Autowired
    private BisAcciInveMapper bisAcciInveMapper;

    @Autowired
    private BisAcciExpRepMapper expRepMapper;

    /**
     * 查询事故对象信息
     * @param guid 查询条件
     * @return 事故对象信息
     */
    @Override
    public ObjAcci selectObjAcciByGuid(String guid) {
        return objAcciMapper.selectObjAcciByGuid(guid);
    }

    /**
     * 查询事故对象列表
     * @param objAcciVo 事故对象查询条件
     * @return 事故对象集合
     */
    @Override
    public List<ObjAcciVo> selectObjAcciList(ObjAcciVo objAcciVo) {
        return objAcciMapper.selectObjAcciList(objAcciVo);
    }

    /**
     * 新增事故对象
     * @param objAcci 事故对象信息
     * @return 影响行数
     */
    @Override
    public int insertObjAcci(ObjAcci objAcci) {
        return objAcciMapper.insertObjAcci(objAcci);
    }

    /**
     * 修改事故对象
     * @param objAcci 事故对象信息
     * @return 影响行数
     */
    @Override
    public int updateObjAcci(ObjAcci objAcci) {
        return objAcciMapper.updateObjAcci(objAcci);
    }

    /**
     * 删除事故对象
     * @param guid 事故对象主键
     * @return 影响行数
     */
    @Override
    public int deleteObjAcciByGuid(String guid) {
        return objAcciMapper.deleteObjAcciByGuid(guid);
    }

    /**
     * 批量删除事故对象
     * @param guids 事故对象主键数组
     * @return 影响行数
     */
    @Override
    public int deleteObjAcciByGuids(String[] guids) {
        return objAcciMapper.deleteObjAcciByGuids(guids);
    }

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

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

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

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

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

    @Override
    public int compoundSave(Map<String, Object> params) {
        ObjAcci acci = new ObjAcci();
        acci.setGuid((String) params.get("guid"));
        acci.setSgdwName((String) params.get("sgdwName"));
        acci.setAcciWiunType((String) params.get("acciWiunType"));
        acci.setAcciCate((String) params.get("acciCate"));
        acci.setOccuTime((String) params.get("occuTime"));
        acci.setSerInjNum((String) params.get("serInjNum"));
        acci.setCasNum((String) params.get("casNum"));
        acci.setEconLoss((String) params.get("econLoss"));
        acci.setAcciGrad((String) params.get("acciGrad"));
        acci.setOtlcolGuuiVicm((String) params.get("otlcolGuuiVicm"));
        String engName = (String) params.get("engName");
        if (engName != null && !engName.isEmpty()) acci.setEngGuid(engName);
        int result;
        if (acci.getGuid() != null && !acci.getGuid().isEmpty()) {
            result = objAcciMapper.updateObjAcci(acci);
        } else {
            acci.setGuid(java.util.UUID.randomUUID().toString().replace("-", ""));
            result = objAcciMapper.insertObjAcci(acci);
        }
        BisAcciInve inve = new BisAcciInve();
        inve.setAcciGuid(acci.getGuid());
        String ifDuty = (String) params.get("ifDuty");
        String ifPhone = (String) params.get("ifPhone");
        if ((ifDuty != null && !ifDuty.isEmpty()) || (ifPhone != null && !ifPhone.isEmpty())) {
            inve.setIfRespAcci(ifDuty);
            inve.setIfPhoRep(ifPhone);
            BisAcciInve query = new BisAcciInve();
            query.setAcciGuid(acci.getGuid());
            java.util.List<BisAcciInve> existing = bisAcciInveMapper.selectBisAcciInveList(query);
            if (existing != null && !existing.isEmpty()) {
                inve.setGuid(existing.get(0).getGuid());
                bisAcciInveMapper.updateBisAcciInve(inve);
            } else {
                inve.setGuid(java.util.UUID.randomUUID().toString().replace("-", ""));
                bisAcciInveMapper.insertBisAcciInve(inve);
            }
        }
        Object progListObj = params.get("progList");
        if (progListObj instanceof java.util.List) {
            java.util.List<Map<String, Object>> progList = (java.util.List<Map<String, Object>>) progListObj;
            for (Map<String, Object> prog : progList) {
                BisAcciExpRep rep = new BisAcciExpRep();
                rep.setAcciGuid(acci.getGuid());
                rep.setRepTime((String) prog.get("repTime"));
                rep.setNote((String) prog.get("acciDesc"));
                rep.setGuid(java.util.UUID.randomUUID().toString().replace("-", ""));
                expRepMapper.insertBisAcciExpRep(rep);
            }
        }
        return result;
    }
}
