package com.bonus.hn.bearing.capacity.backstage.project.service;

import com.bonus.hn.bearing.capacity.backstage.manager.utils.DateTimeHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.project.dao.ProgressDao;
import com.bonus.hn.bearing.capacity.backstage.project.entity.FileBean;
import com.bonus.hn.bearing.capacity.backstage.project.entity.ProgressBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author bonus
 */
@Service("ProgressService")
public class ProgressServiceImpl implements ProgressService {
    @Resource
    private ProgressDao dao;

    @Override
    public List<ProgressBean> getBasicDataList(ProgressBean o) {
        o.setLevel(UserUtil.getLoginUser().getLevel());
        o.setOrgIds(UserUtil.getLoginUser().getOrgId());
        return dao.getBasicDataList(o);
    }

    @Override
    public ProgressBean getProTitleData(ProgressBean o) {
        return dao.getProTitleData(o);
    }

    @Override
    public Map<String, Object> getTowerData(ProgressBean o) {
        Map<String, Object> map = new HashMap<>(100);
        //基础完成-未完成个数.组塔完成-未完成个数
        ProgressBean b = dao.getBasicTowerNum(o);
        //基础数据
        List<ProgressBean> basicList = dao.getBasicList(o);
        //组塔数据
        List<ProgressBean> towerList = dao.getTowerList(o);
        //架线数据
        ProgressBean lineBean = dao.getLineBean(o);
        map.put("numInfo", b);
        map.put("lineData", lineBean);
        map.put("basicData", basicList);
        map.put("towerData", towerList);
        return map;
    }

    @Override
    @Transactional
    public int updateProProgress(ProgressBean bean) {
        int num = 0;
        if (!StringHelper.isEmpty(bean.getBasicDataId())) {
            String[] array = bean.getBasicDataId().split(",");
            for (int i = 0; i < array.length; i++) {
                num = dao.updateTowerStatus(bean.getProId(), array[i], "1", DateTimeHelper.getNowDate());
            }
            num = dao.insertRecord(bean.getProId(), bean.getBasicRemark(),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        if (!StringHelper.isEmpty(bean.getTowerDataId())) {
            String[] array = bean.getTowerDataId().split(",");
            for (int i = 0; i < array.length; i++) {
                num = dao.updateTowerStatus(bean.getProId(), array[i], "2",DateTimeHelper.getNowDate());
            }
            num =  dao.insertRecord(bean.getProId(), bean.getTowerRemark(),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        if (!StringHelper.isEmpty(bean.getChangeLength())) {
            num =  dao.updateLineLength(bean.getProId(), bean.getChangeLength());
            num = dao.insertRecord(bean.getProId(), StringHelper.isEmpty(bean.getLineRemark()) ? ("架线完成" +bean.getChangeLength() +"km"): (bean.getLineRemark() + ";" + "架线完成" +bean.getChangeLength() +"km"),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        num = dao.updateBPProgress(bean.getProId(), bean.getProgress());
        return num;
    }

    @Override
    public Map<String, Object> getProcessTitles(ProgressBean o) {
        Map<String, Object> map = new HashMap<>(100);
        //基础完成-未完成个数.组塔完成-未完成个数
        ProgressBean b = dao.getBasicTowerNum(o);
        //架线数据
        ProgressBean lineBean = dao.getLineBean(o);
        map.put("numInfo", b);
        map.put("lineBean", lineBean);
        return map;
    }

    @Override
    public List<ProgressBean> getProjectProgress(ProgressBean o) {
        return dao.getProjectProgress(o);
    }

    @Override
    public Map<String, Object> getTowerRecordTimeLine(ProgressBean o) {
        Map<String, Object> map = new HashMap<>(100);
        List<ProgressBean> list = dao.getTowerRecordTimeLine(o);
        map.put("ProgressBean", list);
        return map;
    }

    @Override
    public List<ProgressBean> getPersonList(ProgressBean o) {
        return dao.getPersonList(o);
    }

    @Override
    public void insertFile(FileBean bean) {
        dao.insertFile(bean);
    }

    @Override
    public List<FileBean> getFileList(FileBean o) {
        return dao.getFileList(o);
    }

    @Override
    public int delFileRecordById(FileBean bean) {
        return dao.delFileRecordById(bean);
    }

    @Override
    public List<ProgressBean> getExportList(ProgressBean b) {
        b.setLevel(UserUtil.getLoginUser().getLevel());
        b.setOrgIds(UserUtil.getLoginUser().getOrgId());
        return dao.getExportList(b);
    }

    /**
     * @param o
     * @return
     */
    @Override
    public Map<String, Object> getBackspaceTowerData(ProgressBean o) {
        Map<String, Object> map = new HashMap<>(100);
        //基础完成-未完成个数.组塔完成-未完成个数
        ProgressBean b = dao.getBasicTowerNum(o);
        //基础数据
        List<ProgressBean> basicList = dao.getBackspaceBasicList(o);
        //组塔数据
        List<ProgressBean> towerList = dao.getBackspaceTowerList(o);
        //架线数据
        ProgressBean lineBean = dao.getLineBean(o);
        map.put("numInfo", b);
        map.put("lineData", lineBean);
        map.put("basicData", basicList);
        map.put("towerData", towerList);
        return map;
    }

    /**
     * @param bean
     * @return
     */
    @Override
    public int updateBackspaceProProgress(ProgressBean bean) {
        int num = 0;
        if (!StringHelper.isEmpty(bean.getBasicDataId())) {
            String[] array = bean.getBasicDataId().split(",");
            for (int i = 0; i < array.length; i++) {
                num = dao.updateBackspaceTowerStatus(bean.getProId(), array[i], "1", DateTimeHelper.getNowDate());
            }
            num = dao.insertRecord(bean.getProId(), bean.getBasicRemark(),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        if (!StringHelper.isEmpty(bean.getTowerDataId())) {
            String[] array = bean.getTowerDataId().split(",");
            for (int i = 0; i < array.length; i++) {
                num = dao.updateBackspaceTowerStatus(bean.getProId(), array[i], "2",DateTimeHelper.getNowDate());
            }
            num =  dao.insertRecord(bean.getProId(), bean.getTowerRemark(),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        if (!StringHelper.isEmpty(bean.getChangeLength())) {
            num =  dao.updateLineLength(bean.getProId(), bean.getChangeLength());
            num = dao.insertRecord(bean.getProId(), StringHelper.isEmpty(bean.getLineRemark()) ? ("架线回退" +bean.getChangeLength() +"km"): (bean.getLineRemark() + ";" + "架线回退" +bean.getChangeLength() +"km"),bean.getProgress(), UserUtil.getLoginUser().getUsername());
        }
        num = dao.updateBPProgress(bean.getProId(), bean.getProgress());
        return num;
    }
}
