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

import java.util.ArrayList;
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 org.springframework.transaction.annotation.Transactional;
import com.goldenwater.slaj.hidd.mapper.BisOrgMonRepPeriMapper;
import com.goldenwater.slaj.hidd.mapper.BisHiddRecRepMapper;
import com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri;
import com.goldenwater.slaj.hidd.domain.BisHiddRecRep;
import com.goldenwater.slaj.hidd.service.IBisOrgMonRepPeriService;

/**
 * 月报期间 服务层实现
 */
@Service
public class BisOrgMonRepPeriServiceImpl implements IBisOrgMonRepPeriService {

    @Autowired
    private BisOrgMonRepPeriMapper bisOrgMonRepPeriMapper;

    @Autowired
    private BisHiddRecRepMapper bisHiddRecRepMapper;

    @Override
    public BisOrgMonRepPeri selectBisOrgMonRepPeriByGuid(String guid) {
        return bisOrgMonRepPeriMapper.selectBisOrgMonRepPeriByGuid(guid);
    }

    @Override
    public List<BisOrgMonRepPeri> selectBisOrgMonRepPeriList(BisOrgMonRepPeri bisOrgMonRepPeri) {
        return bisOrgMonRepPeriMapper.selectBisOrgMonRepPeriList(bisOrgMonRepPeri);
    }

    @Override
    public int insertBisOrgMonRepPeri(BisOrgMonRepPeri bisOrgMonRepPeri) {
        return bisOrgMonRepPeriMapper.insertBisOrgMonRepPeri(bisOrgMonRepPeri);
    }

    @Override
    public int updateBisOrgMonRepPeri(BisOrgMonRepPeri bisOrgMonRepPeri) {
        return bisOrgMonRepPeriMapper.updateBisOrgMonRepPeri(bisOrgMonRepPeri);
    }

    @Override
    public int deleteBisOrgMonRepPeriByGuid(String guid) {
        return bisOrgMonRepPeriMapper.deleteBisOrgMonRepPeriByGuid(guid);
    }

    @Override
    public int deleteBisOrgMonRepPeriByGuids(String[] guids) {
        return bisOrgMonRepPeriMapper.deleteBisOrgMonRepPeriByGuids(guids);
    }

    @Override
    public List<Map<String, Object>> selectRepTreeData(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>> selectMonthTotalList(Map<String, Object> params) {
        return bisOrgMonRepPeriMapper.selectMonthTotalList(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitMonth(String guid, String[] hiddGuids) {
        BisOrgMonRepPeri period = bisOrgMonRepPeriMapper.selectBisOrgMonRepPeriByGuid(guid);
        if (period == null) {
            throw new RuntimeException("月报期间不存在");
        }
        if ("1".equals(period.getRepAct())) {
            throw new RuntimeException("本月数据已上报，不允许重复上报");
        }
        // 删除该月报下原有上报记录
        bisHiddRecRepMapper.deleteBisHiddRecRepByRepGuid(guid);
        // 新增上报记录
        if (hiddGuids != null) {
            for (String hiddGuid : hiddGuids) {
                BisHiddRecRep rec = new BisHiddRecRep();
                rec.setHiddGuid(hiddGuid);
                rec.setRepGuid(guid);
                rec.setRepOrgGuid(period.getRepOrgGuid());
                rec.setRepAct("1");
                rec.setCollTime(java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                bisHiddRecRepMapper.insertBisHiddRecRep(rec);
            }
        }
        // 更新月报状态为已上报
        period.setRepAct("1");
        period.setUpdTime(java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        bisOrgMonRepPeriMapper.updateBisOrgMonRepPeri(period);
    }

    @Override
    public int returnMonth(String guid, String returnDesc) {
        BisOrgMonRepPeri period = new BisOrgMonRepPeri();
        period.setGuid(guid);
        period.setRepAct("3");
        period.setNote(returnDesc);
        period.setUpdTime(java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return bisOrgMonRepPeriMapper.updateBisOrgMonRepPeri(period);
    }

    @Override
    public int revokeMonth(String guid) {
        // 清除上报记录
        bisHiddRecRepMapper.deleteBisHiddRecRepByRepGuid(guid);
        // 更新月报状态为已撤销
        BisOrgMonRepPeri period = new BisOrgMonRepPeri();
        period.setGuid(guid);
        period.setRepAct("2");
        period.setUpdTime(java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return bisOrgMonRepPeriMapper.updateBisOrgMonRepPeri(period);
    }

    @Override
    public int approveRevoke(String guid, boolean approved, String opinion) {
        BisOrgMonRepPeri period = new BisOrgMonRepPeri();
        period.setGuid(guid);
        period.setRepAct(approved ? "2" : "1");
        period.setNote(opinion);
        period.setUpdTime(java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return bisOrgMonRepPeriMapper.updateBisOrgMonRepPeri(period);
    }
}
