/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.picking.activiti.service;

import com.jeeplus.common.config.Global;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.UploadImages;
import com.jeeplus.modules.sg.managementcenter.activiti.utils.MyActiviUtils;
import com.jeeplus.modules.sg.picking.activiti.entity.*;
import com.jeeplus.modules.sg.picking.activiti.mapper.PickIngMapper;
import com.jeeplus.modules.sys.entity.DictValue;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.security.cert.LDAPCertStoreParameters;
import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 模块显示Service
 *
 * @author
 * @version 2019-11-08
 */
@Service
@Transactional(readOnly = true)
public class PickIngService extends CrudService<PickIngMapper, PickIng> {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private PickIngMapper pickIngMapper;

	@Override
	public Page<PickIng> findPage(Page<PickIng> page, PickIng entity) {
		Page<PickIng> page1 = super.findPage(page, entity);
		return page1;
	}

	//清空临时表数据
	@Transactional(readOnly = false)
	public void truncateTemporary() {
		pickIngMapper.truncateTemporary();
	}

	/**
	 * 遍历查询结构 大类 中类 小类
	 * 去除重复，记录大类
	 * 根据大类查询它的中类放入list并记录
	 */
	public List<PickList> newAllList(PickIng entity, String s) {
		DictValue dic = MyActiviUtils.findDic();
		entity.setVersion(dic.getValue());
		//查询除所有数据
		List<PickIng> allList = pickIngMapper.findAllList(entity);
		//创建返回对象集合
		List<PickList> returnList = new ArrayList<>();

		//循环得到所有大类
		for (PickIng pickIng : allList) {
			boolean flag = false;
			for (PickList list : returnList) {
				if (pickIng.getMdBigClass().equals(list.getName())) {
					flag = true;
					break;
				}
			}
			if (flag == false) {
				//创建返回对象
				PickList pickList = new PickList();
				pickList.setName(pickIng.getMdBigClass());
				returnList.add(pickList);
			}
		}

		//循环所有的大类，与查出的数据做比对，相同的就放入同一个实体类
		for (PickList pList : returnList) {
			List<MdTheClass> mdTheClassList = new ArrayList<>();
			//获得大类
			String name = pList.getName();
			//遍历查询的所有数据，与大类做比对，相同的，将中类记录下来
			for (PickIng pickIng : allList) {
				boolean flag = false;
				//如果查出的数据中和记录下的大类一致，将中类放入大类的
				if (pickIng.getMdBigClass().equals(name)) {
					for (MdTheClass md : mdTheClassList) {
						if (md.getName().equals(pickIng.getMdTheClass())) {
							flag = true;
						}
					}
					if (flag == false) {
						//创建中类对象，包含种类名称及对应的小类集合
						MdTheClass mdTheClass = new MdTheClass();
						mdTheClass.setId(pickIng.getId());
						mdTheClass.setName(pickIng.getMdTheClass());
						mdTheClassList.add(mdTheClass);
					}
				}
			}
			pList.setMdTheClassList(mdTheClassList);
		}

		for (PickList pList : returnList) {
			List<MdTheClass> mdTheClassList = pList.getMdTheClassList();
			for (MdTheClass md : mdTheClassList) {
				//创建
				List<MdSmallClass> mdSmallClassList = new ArrayList<>();
				for (PickIng pc : allList) {
					if (md.getName().equals(pc.getMdTheClass()) && pList.getName().equals(pc.getMdBigClass())) {
						//创建小类对象
						MdSmallClass mdSmallClass = new MdSmallClass();
						mdSmallClass.setName(pc.getMdSmallClass());
						mdSmallClass.setId(pc.getId());
						mdSmallClass.setMdUnit(pc.getMdUnit());
						if (Global.SYS_TYPE.equals("2")) {
							if (pc.getMdImg() != null && !"".equals(pc.getMdImg())) {
								mdSmallClass.setMdImg("http://" + request.getServerName() + pc.getMdImg());
							}
						} else {
							mdSmallClass.setMdImg(pc.getMdImg());
						}
						mdSmallClassList.add(mdSmallClass);
					}
					md.setMdSmallClassList(mdSmallClassList);
				}
			}
		}
		return returnList;
	}


	public List<String> allVersion() {
		return pickIngMapper.allVersion();
	}

	@Transactional(readOnly = false)
	public void updateSelectNumber(String selectNumber, String procInsId) {
		pickIngMapper.updateSelectNumber(selectNumber, procInsId);
	}

	public String findSelectNumber(String procInsId) {
		return pickIngMapper.findSelectNumber(procInsId);
	}

	public String selectVersion(String procInsId) {
		return pickIngMapper.selectVersion(procInsId);
	}

	public ProjectNumber projectNumber(String procInsId) {
		return pickIngMapper.projectNumber(procInsId);
	}

	@Transactional(readOnly = false)
	public void updateSelectVersion(String version) {
		pickIngMapper.updateSelectVersion(version);
	}

	;

	@Transactional(readOnly = false)
	public void processList(List<ShowList> showList) {
		pickIngMapper.processList(showList);
	}

	/**
	 * 清空所有临时表的数据
	 */
	@Transactional(readOnly = false)
	public void delAllTem() {
		pickIngMapper.delAllTem();
	}

	/**
	 * 插入临时领料清单
	 */
	@Transactional(readOnly = false)
	public void processTemList(@Param("showList") List<ShowList> showList) {
		pickIngMapper.processTemList(showList);
	}

	/**
	 * 插入临时领料清单：物资上报
	 */
	@Transactional(readOnly = false)
	public void processTemReportList(@Param("showList") List<ShowList> showList) {
		pickIngMapper.processTemReportList(showList);
	}

	public List<Acquisition> findErpType(List<String> list) {
		return pickIngMapper.findErpType(list);
	}

	public List<Acquisition> findErpAll(List<String> list) {
		return pickIngMapper.findErpAll(list);
	}


	/**
	 * 成本项目导出
	 */
	@Transactional(readOnly = false)
	public List<CapitalExport> cbList(List<Acquisition> list, String projectName) {
		List<CapitalExport> capitalExports = pickIngMapper.exportList(list, projectName);
		int i = 0;
		for (CapitalExport c : capitalExports) {
			//项目定义号
			String projectWbsNumber = c.getProjectWbsNumber();
			i = i + 1;
			c.setErpNumber(String.valueOf(i));
			c.setProjectWbsNumber(projectWbsNumber + "0010000000");
			c.setWbsNumber(projectWbsNumber + "0012000000");
			c.setWbsDescription("配网检修-外包材料费");
			c.setDeviceSerialNumber("999999");
			c.setModelSpecification(c.getNameOfBuilding());
			Calendar date = Calendar.getInstance();
			String year = String.valueOf(date.get(Calendar.YEAR));
			c.setDemandDate(year + "1230");
			c.setDeliveryPlace("买方指定仓库车板交货");
			c.setDemandDepartment("建设部（项目管理中心）");
			c.setApplicant("沈忱");
			c.setPhoneNumber("0511-84028133");
			c.setIdOfCommerce("A448-500001083-00004");
		}
		updateErpAfter(list);
		return capitalExports;
	}

	/**
	 * 资本项目导出
	 */
	@Transactional(readOnly = false)
	public List<CapitalExport> zbList(List<Acquisition> list, String projectName) {
		List<CapitalExport> capitalExports = pickIngMapper.exportList(list, projectName);
		int i = 0;
		for (CapitalExport c : capitalExports) {
			//项目定义号
			String projectWbsNumber = c.getProjectWbsNumber();
			String deviceSerialNumber = c.getDeviceSerialNumber();
			i = i + 1;
			c.setErpNumber(String.valueOf(i));
			c.setProjectWbsNumber(projectWbsNumber + "00A0000000");
			c.setWbsNumber(projectWbsNumber + "00A1310000");
			c.setWbsDescription("变压器");
			if (deviceSerialNumber != null && !"".equals(deviceSerialNumber)) {
				if (deviceSerialNumber.contains("变压器")) {
					c.setDeviceSerialNumber("1");
					c.setEquipmentType("1302001");
					c.setUseEnvironment("户外");
				} else if (deviceSerialNumber.contains("环网柜") || deviceSerialNumber.contains("环网箱")) {
					c.setDeviceSerialNumber("2");
					c.setEquipmentType("1302004");
					c.setUseEnvironment("户外");
				} else if (deviceSerialNumber.contains("箱式变电站")) {
					c.setDeviceSerialNumber("3");
					c.setEquipmentType("1302003");
					c.setUseEnvironment("户外");
				} else if (deviceSerialNumber.contains("高压开关柜")) {
					c.setDeviceSerialNumber("4");
					c.setEquipmentType("1302010");
					c.setUseEnvironment("户外");
				} else {
					c.setDeviceSerialNumber("999999");
				}
			} else {
				c.setDeviceSerialNumber("");
			}
			c.setModelSpecification(c.getNameOfBuilding());
			Calendar date = Calendar.getInstance();
			String year = String.valueOf(date.get(Calendar.YEAR));
			c.setDemandDate(year + "1230");
			c.setDeliveryPlace("买方指定仓库车板交货");
			c.setDemandDepartment("建设部（项目管理中心）");
			c.setApplicant("沈忱");
			c.setPhoneNumber("0511-84028133");
			c.setIdOfCommerce("A448-500001083-00004");
		}
		updateErpAfter(list);
		return capitalExports;
	}

	@Transactional(readOnly = false)
	public void updateErpAfter(List<Acquisition> list) {
		pickIngMapper.updateErpAfter(list);
	}

	public List<ExportBlock> findExportBlock(String procInsId) {
		return pickIngMapper.findExportBlock(procInsId);
	}

	//根据流程实例id查询所有符合的临时表数据
	public List<ShowList> findAllTem(String procInsId) {
		return pickIngMapper.findAllTem(procInsId);
	}

	//根据流程定义号查询当前 模块清单进行回显
	public List<ShowList> findProcInsIdMaterialReport(String procInsId) {
		return pickIngMapper.findProcInsIdMaterialReport(procInsId);
	}

	//根据流程定义号删除当前临时表数据
	@Transactional(readOnly = false)
	public Integer deleteProcInsIdMaterialReport(String procInsId) {
		return pickIngMapper.deleteProcInsIdMaterialReport(procInsId);
	}

	//根据项目定义号删除当前流程表
	@Transactional(readOnly = false)
	public Integer deleteProcInsIdPickingApplication(String procInsId) {
		return pickIngMapper.deleteProcInsIdPickingApplication(procInsId);
	}

	//根据项目定义号获取
	@Transactional(readOnly = false)
	public void updateEnter(String procInsId) {
		pickIngMapper.updateEnter(procInsId);
		pickIngMapper.updateListEnter(procInsId);
	}

	@Transactional(readOnly = false)
	public void updateEnterList(List<Acquisition> acquisitions) {
		pickIngMapper.updateEnterList(acquisitions);
	}

	@Transactional(readOnly = false)
	public void updateProjectEnter(List<String> updateProjectList) {
		pickIngMapper.updateProjectEnter(updateProjectList);
	}
}