package com.nantian.cloud.jljc.service.impl;

import com.nantian.cloud.jljc.entity.TDualcontrolIndex;
import com.nantian.cloud.jljc.mapper.TDualcontrolIndexMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.mapper.TDualcontrolIndexresolveMapper;
import com.nantian.cloud.jljc.service.TDualcontrolIndexService;
import com.nantian.cloud.jljc.vo.dualControl.DualcontrolIndexTreeVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.nantian.cloud.jljc.vo.util.TreeVoUtil.findTree;

/**
 * 能耗双控-指标 服务实现类
 */
@Service("tDualcontrolIndexService")
public class TDualcontrolIndexServiceImpl extends ServiceImpl<TDualcontrolIndexMapper, TDualcontrolIndex> implements TDualcontrolIndexService {

    @Override
    public List<DualcontrolIndexTreeVo> getIndexTree(String type, String plan, String year) {
        String indexYear = "1".equals(type) ? plan : year;
        ScUser user = SecurityUtils.getUser();
        String deptId  = user.getDeptId().toString();
        String regionId = deptId.replaceAll("0000","").replaceAll("00","");
        List<DualcontrolIndexTreeVo> treeVos = baseMapper.getIndexTree(indexYear,deptId,regionId);
        return  findTree(treeVos);
    }

    @Override
    public List<TDualcontrolIndex> getIndexByVersion(long versionId, String year) {
        return getIndexList(versionId, year);
    }

    @Override
    public R saveIndexByVersion(long versionId, String year) {
        List<TDualcontrolIndex> indexList = getIndexList(versionId, year);
        ScUser user = SecurityUtils.getUser();
        if(indexList!=null && indexList.size()>0){
            int num = 0;
            for (TDualcontrolIndex index : indexList) {
                index.setUserid(user.getId());
                index.setDeptId(user.getDeptId());
                index.setDelFlag("1");
                index.setUpdateTime(new Date());
                num += baseMapper.insert(index);
            }
            return R.ok("下发指标成功，累计下发"+num+"条指标");
        }else{
            return R.failed("当前版本无指标数据，指标下发失败");
        }
    }

    @Override
    public TDualcontrolIndex getIndex(String year, String itemCode) {
        return baseMapper.getIndex(year,itemCode);
    }

    private List<TDualcontrolIndex> getIndexList(long versionId, String year) {
        String deptId  = SecurityUtils.getUser().getDeptId().toString();
        Integer num = 0;
        if(year.contains("-")){ //查询五年指标
            num = 0;
        }else{
            num = Integer.parseInt(year)%5==0 ? 5 : Integer.parseInt(year)%5;
        }
        List<TDualcontrolIndex> indexList = baseMapper.getIndexByVersion(deptId,versionId,year,num.toString());
        return indexList;
    }
}
