package cn.com.victorysoft.business.sys.service;

import cn.com.victorysoft.business.constant.UnitColumn;
import cn.com.victorysoft.business.sys.dao.*;
import cn.com.victorysoft.business.sys.entity.*;
import cn.com.victorysoft.business.sys.param.UnitFieldBlockWellParam;
import cn.com.victorysoft.business.sys.param.UnitGroupWellParam;
import cn.com.victorysoft.business.sys.param.UnitWellParam;
import cn.com.victorysoft.business.util.PageUtils;
import cn.com.victorysoft.business.util.StringUtils;
import cn.com.victorysoft.business.util.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysBaseService {

    @Resource
    private SysUnitinfoDao sysUnitinfoDao;

    @Resource
    private WellBasicDao wellBasicDao;

    @Resource
    private SysFieldinfoDao sysFieldinfoDao;

    @Resource
    private SysBlockinfoDao sysBlockinfoDao;

    @Resource
    private WellGroupDao wellGroupDao;

    /**
     * 查询本单位及下属单位列表
     */
    public List<SysUnitinfo> selectUnitList(String unitcode) {
        return sysUnitinfoDao.selectUnitTree(unitcode);
    }
    /**
     * 查询本单位及下属单位，组成树形结构
     */
    public SysUnitinfo selectUnitTree(String unitcode) {
        List<SysUnitinfo> list;
        if ("30200000".equals(unitcode)) {
            list = sysUnitinfoDao.selectUnitAll();
        }else {
            list = sysUnitinfoDao.selectUnitTree(unitcode);
        }
        return buildUnitTree(list);
    }

    /**
     * 查询本单位及下属单位列表，查询到采油厂
     */
    public List<SysUnitinfo> selectUnitNodeListLevel2(String unitcode) {
        return sysUnitinfoDao.selectUnitTreeLevel2(unitcode);
    }

    /**
     * 查询本单位及下属单位，组成树形结构，查询到采油厂
     */
    public SysUnitinfo selectUnitTreeLevel2(String unitcode) {
        List<SysUnitinfo> list = sysUnitinfoDao.selectUnitTreeLevel2(unitcode);
        return buildUnitTree(list);
    }

    /**
     * 查询本单位及下属单位列表，查询到管理区
     */
    public List<SysUnitinfo> selectUnitNodeListLevel3(String unitcode) {
        return sysUnitinfoDao.selectUnitTreeLevel3(unitcode);
    }

    /**
     * 查询本单位及下属单位，组成树形结构，查询到管理区
     */
    public SysUnitinfo selectUnitTreeLevel3(String unitcode) {
        List<SysUnitinfo> list = sysUnitinfoDao.selectUnitTreeLevel3(unitcode);
        return buildUnitTree(list);
    }

    /**
     * 查询下属管理区
     */
    public List<SysUnitinfo> selectUnitListLevel3(String unitcode) {
        List<SysUnitinfo> list = sysUnitinfoDao.selectUnitTreeLevel3(unitcode);
        list = list.stream().filter(unit -> unit.getUnitlevel().intValue() == 3).collect(Collectors.toList());
        list.forEach(unit -> unit.setFullname(unit.getFullname().replace("分公司/", "")));
        return list;
    }

    public List<SysUnitinfo> selectUnitChildren(String unitcode) {
        return sysUnitinfoDao.selectChildren(unitcode);
    }

    public SysUnitinfo selectUnitByCode(String unitcode) {
        return sysUnitinfoDao.selectUnitByCode(unitcode);
    }

    /**
     * 构建单位树
     */
    public SysUnitinfo buildUnitTree(List<? extends SysUnitinfo> list) {
        if (list == null || list.isEmpty()) return null;
        SysUnitinfo root = list.get(0);
        Map<String, SysUnitinfo> map = list.stream().collect(Collectors.toMap(SysUnitinfo::getUnitcode, a -> a));
        list.forEach(unit -> {
            SysUnitinfo parent = map.get(unit.getPUnitcode());
            if (parent == null) return;
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList<>());
            }
            parent.getChildren().add(unit);
        });
        return root;
    }

    /**
     * 根据单位查询下属抽油机井号
     * @param param 单位/油田/区块/井号参数
     * @return 井号列表，只返回20条
     */
    public List<WellBasic> selectWaterWellTop20(UnitFieldBlockWellParam param) {
        if ("30200000".equals(param.getUnitcode())) {
            param.setUnitcode(null);
        }
        PageHelper.startPage(1, 20);
        return wellBasicDao.selectWaterWells(param);
    }

    /**
     * 根据井号查询基础信息
     * @param wellname 井号
     * @return 井基础信息
     */
    public WellBasic selectWellBasic(String wellname) {
        return wellBasicDao.selectWellBasic(wellname);
    }


    /**
     * 查询油田列表
     * @param unitcode 单位代码
     */
    public List<SysFieldinfo> selectFieldList(String unitcode){
        return sysFieldinfoDao.selectByUnit(unitcode);
    }

    /**
     * 查询区块列表
     * @param unitcode 单位代码
     * @param fieldcode 油田代码
     */
    public List<SysBlockinfo> selectBlockList(String unitcode, String fieldcode){
        return sysBlockinfoDao.selectByUnitField(unitcode, fieldcode);
    }

    /**
     * 查询区块列表
     */
    public List<SysBlockinfo> selectBlockListTop20(String unitcode, String blockname){
        // 需求变更，改为查询全部
//        PageHelper.startPage(1, 20);
        return sysBlockinfoDao.selectByUnitBlock(unitcode, blockname);
    }

    /**
     * 根据单位代码获取子单位的列名
     */
    public String getUnitColumn(String unitCode) {
        SysUnitinfo unit = sysUnitinfoDao.selectUnit(unitCode);
        return UnitColumn.levelToColumn.get(unit.getUnitlevel().intValue() + 1);
    }

    /**
     * 根据单位代码获取本单位及子单位的列名
     */
    public String[] getUnitColumns(String unitCode) {
        SysUnitinfo unit = sysUnitinfoDao.selectUnit(unitCode);
        String[] result = new String[2];
        int level = unit.getUnitlevel().intValue();
        result[0] = UnitColumn.levelToColumn.get(level);
        result[1] = UnitColumn.levelToColumn.get(level == 4 ? 4 : level + 1);
        return result;
    }

    /**
     * 查询本单位
     */
    public SysUnitinfo getUnitinfo(String unitCode) {
        return sysUnitinfoDao.selectUnit(unitCode);
    }

    /**
     * 查询所属采油厂
     * @param unitcode 单位代码
     */
    public SysUnitinfo selectParentCompany(String unitcode) {
        return sysUnitinfoDao.selectParentCompany(unitcode);
    }

    /**
     * 查询未划分井组的井号
     * @param unitcode 单位代码
     * @return 单位下未划分井组的井号
     */
    public List<WellBasic> selectNoGroupWellList(String unitcode) {
        return wellBasicDao.selectNoGroupWellList(unitcode);
    }

    /**
     * 井组-查询井组列表
     */
    public PageInfo<WellGroup> selectWellGroupList(UnitGroupWellParam param){
        PageUtils.startPage(param);
        List<WellGroup> list =  wellGroupDao.selectWellGroupList(param);
        return new PageInfo<>(list);
    }


    /**
     * 井组-查询井组对象
     */
    public WellGroup selectWellGroup(String id){
        WellGroup wellGroup =  wellGroupDao.selectByPrimaryKey(id);
        if (wellGroup != null) {
            if (StringUtils.isNotEmpty(wellGroup.getOilWells())) {
                wellGroup.setOilWellList(Arrays.asList(wellGroup.getOilWells().split(",")));
            }
            if (StringUtils.isNotEmpty(wellGroup.getWaterWells())) {
                wellGroup.setWaterWellList(Arrays.asList(wellGroup.getWaterWells().split(",")));
            }
        }
        return wellGroup;
    }

    /**
     * 井组-新增井组
     */
    public void insertWellGroup(WellGroup wellGroup){
        wellGroup.setId(UuidUtils.generate());
        if (wellGroup.getOilWellList() != null) {
            wellGroup.setOilWells(String.join(",", wellGroup.getOilWellList()));
            WellBasic wellBasic = wellBasicDao.selectWellBasic(wellGroup.getOilWellList().get(0));
            wellGroup.setUnitcode(wellBasic.getCompanycode());
        }
        if (wellGroup.getWaterWellList() != null) {
            wellGroup.setWaterWells(String.join(",", wellGroup.getWaterWellList()));
            if (StringUtils.isEmpty(wellGroup.getUnitcode())) {
                WellBasic wellBasic = wellBasicDao.selectWellBasic(wellGroup.getWaterWellList().get(0));
                wellGroup.setUnitcode(wellBasic.getCompanycode());
            }
        }
        wellGroupDao.insert(wellGroup);
    }

    /**
     * 井组-更新井组
     */
    public void updateWellGroup(WellGroup wellGroup){
        if (wellGroup.getOilWellList() != null) {
            wellGroup.setOilWells(String.join(",", wellGroup.getOilWellList()));
        }
        if (wellGroup.getWaterWellList() != null) {
            wellGroup.setWaterWells(String.join(",", wellGroup.getWaterWellList()));
        }
        wellGroupDao.updateByPrimaryKeySelective(wellGroup);
    }

    /**
     * 井组-删除井组
     */
    public void deleteWellGroup(String id){
        wellGroupDao.deleteByPrimaryKey(id);
    }

    /**
     * 查询本采油厂列表
     */
    public List<SysUnitinfo> selectCompanyList() {
        return sysUnitinfoDao.selectCompanyList();
    }

    /**
     * 根据油井查询井组数据
     */
    public List<WellBasic> selectGroupWellByOilWellname(String wellname) {
        List<WellBasic> result = new ArrayList<>();
        List<WellGroup> wellGroups = wellGroupDao.selectByOilWellname(wellname);
        if (wellGroups == null || wellGroups.size() == 0) {
            return result;
        }
        WellGroup wellGroup = wellGroups.get(0);
        if (StringUtils.isNotEmpty(wellGroup.getWaterWells())) {
            wellGroup.setWaterWellList(Arrays.asList(wellGroup.getWaterWells().split(",")));
            List<WellBasic> waterWellBasicList = wellBasicDao.selectWellBasicByNames(wellGroup.getWaterWellList());
            result.addAll(waterWellBasicList);
        }
        return result;
    }
}
