package com.bootdo.data.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.service.DictService;
import com.bootdo.data.dao.DataApplyDao;
import com.bootdo.data.dao.DataInfoDao;
import com.bootdo.data.dao.DataRelationDao;
import com.bootdo.data.dao.UnitInfoDao;
import com.bootdo.data.domain.DataInfoDO;
import com.bootdo.data.domain.DataRelationDO;
import com.bootdo.data.domain.UnitApplyInfoDO;
import com.bootdo.data.domain.UnitInfoDO;
import com.bootdo.data.service.UnitInfoService;

@Service
public class UnitInfoServiceImpl implements UnitInfoService {
    @Autowired
    private UnitInfoDao unitInfoDao;

    @Autowired
    private DataRelationDao dataRelationDao;

    @Autowired
    private DataInfoDao dataInfoDao;

    @Autowired
    private DataApplyDao dataApplyDao;

    @Autowired
    private DictService dictService;

    @Override
    public UnitInfoDO get(Long id) {
        return unitInfoDao.get(id);
    }

    @Override
    public List<UnitInfoDO> list(Map<String, Object> map) {
        return unitInfoDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return unitInfoDao.count(map);
    }

    @Override
    public int save(UnitInfoDO unitInfo) {
        return unitInfoDao.save(unitInfo);
    }

    @Override
    public int update(UnitInfoDO unitInfo) {
        return unitInfoDao.update(unitInfo);
    }

    @Override
    public int remove(Long id) {
        return unitInfoDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return unitInfoDao.batchRemove(ids);
    }

    @Override
    public List<UnitApplyInfoDO> listQuery(Map<String, Object> map) {
        List<UnitApplyInfoDO> list = unitInfoDao.listQuery(map);
        for (UnitApplyInfoDO unitApplyInfoDO : list) {
            unitApplyInfoDO.setDataIndustry(dictService.getName("data_industry", unitApplyInfoDO.getDataIndustry()));
        }

        return list;
    }

    @Override
    public int countQuery(Map<String, Object> map) {
        return unitInfoDao.countQuery(map);
    }

    @Override
    public List<UnitApplyInfoDO> listEditInfo(Map<String, Object> map) {
        return unitInfoDao.listEditInfo(map);
    }

    @Override
    public int askRemove(Long id) {
        // 数据表状态修改为 未上报
        //先查询
        updateDataStatus(id);
        // 删除 数据备案与重要数据关系表
        dataRelationDao.removeByApplyId(id);
        // 删除 数据备案表
        return dataApplyDao.remove(id);
    }

    @Override
    public int askBatchRemove(Long[] ids) {
        // 数据表状态修改为 未上报
        for (Long id : ids) {
            updateDataStatus(id);
        }
        // 删除 数据备案与重要数据关系表
        dataRelationDao.batchRemoveByApplyId(ids);
        // 删除 数据备案表
        return dataApplyDao.batchRemove(ids);
    }

    /**
     * @Description: 修改数据状态
     * @Author: wangp
     * @Date: 2022/3/2 0:49
     * @param id
     * @Return: void
     */
    private void updateDataStatus(Long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("applyId",id);
        List<DataRelationDO> list =dataRelationDao.list(map);
        for(DataRelationDO dataRelationDO : list){
            DataInfoDO dataInfo = new DataInfoDO();
            dataInfo.setDataStatus("0");//未上报 正常
            dataInfo.setId(dataRelationDO.getDataId());
            //更新状态
            dataInfoDao.update(dataInfo);
        }
    }

}
