package com.bootdo.data.service.impl;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.service.DictService;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.data.dao.DataApprovalDao;
import com.bootdo.data.dao.DataInfoDao;
import com.bootdo.data.dao.DataRelationDao;
import com.bootdo.data.domain.DataApprovalDO;
import com.bootdo.data.domain.DataInfoDO;
import com.bootdo.data.domain.DataRelationDO;
import com.bootdo.data.domain.UnitApplyInfoDO;
import com.bootdo.data.service.DataApprovalService;
import com.bootdo.system.domain.UserDO;

@Service
public class DataApprovalServiceImpl implements DataApprovalService {

    private static Logger log = LoggerFactory.getLogger(DataApprovalServiceImpl.class);

    @Autowired
    private DataRelationDao dataRelationDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private DataInfoDao dataInfoDao;

    @Autowired
    private DataApprovalDao dataApprovalDao;

    @Override
    public DataApprovalDO get(Long id) {
        return dataApprovalDao.get(id);
    }

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

    @Override
    public List<UnitApplyInfoDO> viewList(Map<String, Object> map) {
        List<UnitApplyInfoDO> list = dataApprovalDao.list(map);
        return list;
    }

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

    @Override
    public int verifyInfo(DataApprovalDO dataApproval) {
        // 备案ID
        long applyId = dataApproval.getApplyId();
        // 审批表写入
        // 先删除
        dataApprovalDao.removeByApplyId(applyId);
        // 在写入
        int count = dataApprovalDao.save(dataApproval);
        // 数据表状态更新
        // dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
        // 数据状态 0:正常 1:删除 2:已上报 3:通过审核 4:驳回
        log.info("ApprovalStatus:" + dataApproval.getApprovalStatus());
        if (dataApproval.getApprovalStatus().equals("0")) {
            updateDataStatus(applyId, "3");// 已审核
        } else if (dataApproval.getApprovalStatus().equals("1")) {
            updateDataStatus(applyId, "4");// 驳回
        } else if (dataApproval.getApprovalStatus().equals("3")) {
            updateDataStatus(applyId, "2");// 取消 恢复数据上报状态
        }
        return count;
    }

    @Override
    public int update(DataApprovalDO dataApproval) {
        return dataApprovalDao.update(dataApproval);
    }

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

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

    @Override
    public boolean approval(Long applyId, String status, String desc) {
        // 获取备案相关信息
        Map<String, Object> params = new HashMap<>();
        params.put("applyId", applyId);
        List<UnitApplyInfoDO> list = dataApprovalDao.list(params);
        UnitApplyInfoDO unitApplyInfoDO = null;
        if (list != null && list.size() > 0) {
            // 获取信息
            unitApplyInfoDO = list.get(0);
            log.info("unitApplyInfoDO:" + unitApplyInfoDO.getId());
            UserDO currUser = ShiroUtils.getUser();
            log.info("currUser:" + currUser.toString());
            DataApprovalDO dataApproval = new DataApprovalDO();
            dataApproval.setApplyId(unitApplyInfoDO.getId());// 备案主键ID
            dataApproval.setApprovalStatus(status);// dataApproval.getStatus() 审批状态 0:通过 1:驳回 3:取消
            dataApproval.setApprovalDesc(desc);
            if (null == currUser) {
                dataApproval.setApprovalUserid("-1");
                dataApproval.setApprovalUser("获取用户信息为空");
            } else {
                dataApproval.setApprovalUserid(String.valueOf(ShiroUtils.getUserId()));
                dataApproval.setApprovalUser(ShiroUtils.getUser().getUsername());
            }
            log.info("dataApproval:" + dataApproval.toString());
            // 审核成功
            if (verifyInfo(dataApproval) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description: 修改数据状态
     * @Author: wangp
     * @Date: 2022/3/2 0:49
     * @param id
     * @Return: void
     */
    private void updateDataStatus(Long id, String status) {
        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(status);// 数据状态 0:正常 1:删除 2:已上报 3:通过审核 4:驳回
            dataInfo.setId(dataRelationDO.getDataId());
            // 更新状态
            dataInfoDao.update(dataInfo);
        }
    }

}
