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

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpCredit;
import com.jeeplus.modules.sg.financial.settlement.entity.CostCheck;
import com.jeeplus.modules.sg.financial.settlement.entity.ProjectBudget;
import com.jeeplus.modules.sg.financial.settlement.mapper.CostCheckMapper;
import com.jeeplus.modules.sg.financial.settlement.mapper.ProjectBudgetMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * 费用检查表Service
 */
@Service
@Transactional(readOnly = true)
public class CostCheckService extends CrudService<CostCheckMapper, CostCheck> {

	@Autowired
	private CostCheckMapper costCheckMapper;

	@Autowired
	private ProjectBudgetService projectBudgetService;

	public static Map<String, CellStyle> styles;

	@Override
	public CostCheck get(String id) {
		return super.get(id);
	}

	@Override
	public List<CostCheck> findList(CostCheck costCheck) {
		return super.findList(costCheck);
	}


	@Override
	public Page<CostCheck> findPage(Page<CostCheck> page, CostCheck costCheck) {
		return super.findPage(page, costCheck);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(CostCheck costCheck) {
		super.save(costCheck);
	}

	@Transactional(readOnly = false)
	public void updateDesignSup(ProjectBudget projectBudget) {
		costCheckMapper.updateDesignSup(projectBudget);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(CostCheck costCheck) {
		super.delete(costCheck);
	}

	@Transactional(readOnly = false)
	public String insertList(@Param("list") List<ProjectBudget> list) {
		String str = "";
		List<ProjectBudget> insertList = new ArrayList<>();
		List<ProjectBudget> updateList = new ArrayList<>();
		//查找所有的费用检查信息
		List<CostCheck> allList = costCheckMapper.findAllProjectId();
		if (allList.size() > 0) {
			for (ProjectBudget b : list) {
				for (CostCheck a : allList) {
					if (a.getProjectId().equals(b.getProjectId())) {
						b.preInsert();
						updateList.add(b);
					}
				}
			}
			//获得需要更新的项目信息，其它项目信息则插入
			Iterator<ProjectBudget> iterator = list.iterator();
			while (iterator.hasNext()) {
				ProjectBudget next = iterator.next();
				for (ProjectBudget u :
						updateList) {
					if (next.getProjectId().equals(u.getProjectId())) {
						iterator.remove();
					}
				}
			}
			if (list.size() > 0) {
				for (ProjectBudget a :
						list) {
					insertList.add(a);
				}
			}
			if (insertList.size() > 0) {
				for (ProjectBudget p :
						insertList) {
					p.preInsert();
				}
			}
		} else {
			for (ProjectBudget b : list) {
				b.preInsert();
				insertList.add(b);
			}
		}
		//插入
		if (insertList.size() > 0) {
			costCheckMapper.insertList(insertList);
			str += "费用检查信息插入" + insertList.size() + "条-";
		}
		//更新
		if (updateList.size() > 0) {
			costCheckMapper.updateList(updateList);
			str += "费用检查信息更新" + updateList.size() + "条-";
		}
		return str;
	}

	@Transactional(readOnly = false)
	public String insertConstructionList(@Param("list") List<CostCheck> list) {
		String str = "";
		List<CostCheck> updateList = new ArrayList<>();
		List<CostCheck> allList = costCheckMapper.findAllProjectId();
		for (CostCheck costCheck : allList) {
			for (CostCheck c : list) {
				if (costCheck.getProjectId().equals(c.getProjectId())) {
					c.preInsert();
					updateList.add(c);
				}
			}
		}
		if (updateList.size() > 0) {
			costCheckMapper.updateConstructionList(updateList);
			str += "概预算信息更新" + updateList.size() + "条";
		} else {
			str += "概预算信息更新0条";
		}
		return str;
	}

	@Transactional(readOnly = false)
	public void updateDes(CostCheck costCheck) {
		costCheckMapper.updateDes(costCheck);
	}

	@Transactional(readOnly = false)
	public void updateCon(CostCheck costCheck) {
		costCheckMapper.updateCon(costCheck);
	}

	@Transactional(readOnly = false)
	public void updateSup(CostCheck costCheck) {
		costCheckMapper.updateSup(costCheck);
	}

	@Transactional(readOnly = false)
	public Map<String, List<String>> verifyProjectsCost(List<ErpCredit> erpCredits) {
		HashMap hashMap = new HashMap();
		costCheckMapper.updatePaid(erpCredits);
		int conFlg;
		List<String> notExitList = new ArrayList<>();
		List<String> notPassList = new ArrayList<>();
		for (ErpCredit e :
				erpCredits) {
			//项目定义号
			conFlg = 0;
			String projectId = e.getItemId();
			CostCheck aLlPaid = costCheckMapper.findALlPaid(projectId);
			if (null != aLlPaid) {
				//施工费应付金额
				Double constructionAmountDue = aLlPaid.getConstructionAmountDue();
				//设计费应付金额
				Double designAmountDue = aLlPaid.getDesignAmountDue();
				//监理费应付金额
				Double supervisorAmountDue = aLlPaid.getSupervisorAmountDue();

				//施工费实付金额
				Double constructionAmountPaid = aLlPaid.getConstructionAmountPaid();
				//设计费实付金额
				Double designAmountPaid = aLlPaid.getDesignAmountPaid();
				//监理费实付金额
				Double supervisorAmountPaid = aLlPaid.getSupervisorAmountPaid();
				//施工费允许少付
				String constructionUnderPayment = aLlPaid.getConstructionUnderPayment();
				//设计费允许少付
				String designUnderPayment = aLlPaid.getDesignUnderPayment();
				//监理费允许少付
				String supervisorUnderPayment = aLlPaid.getSupervisorUnderPayment();
				//判断是否缺少各项应付金额
				if (null == constructionAmountDue
						|| null == designAmountDue
						|| null == supervisorAmountDue) {
					notExitList.add(projectId);
					if (null == constructionAmountDue) {
						projectBudgetService.updateCheck(projectId,"construction_cost_check","2");
						conFlg = 1;
					}
					if (null == designAmountDue) {
						designAmountDue = 0.0;
					}
					if (null == supervisorAmountDue) {
						supervisorAmountDue = 0.0;
					}
				}
				int des = designAmountDue.compareTo(designAmountPaid);
				int sup = supervisorAmountDue.compareTo(supervisorAmountPaid);
				//判断施工费是否相等
				if (conFlg != 1) {
					int con = constructionAmountDue.compareTo(constructionAmountPaid);
					if (con != 0) {
						//不相等，判断是否运行少付
						if ("0".equals(constructionUnderPayment)) {
							notPassList.add(projectId);
							projectBudgetService.updateCheck(projectId,"construction_cost_check","2");
						}else {
							projectBudgetService.updateCheck(projectId,"construction_cost_check","1");
						}
					}else{
						//验证通过
						projectBudgetService.updateCheck(projectId,"construction_cost_check","1");
					}
				}

				//判断设计费是否相等
				if (des != 0) {
					//不相等，判断是否运行少付
					if ("0".equals(designUnderPayment)) {
						notPassList.add(projectId);
						projectBudgetService.updateCheck(projectId,"design_cost_check","2");
					}else {
						projectBudgetService.updateCheck(projectId,"design_cost_check","1");
					}
				}else {
					//验证通过
					projectBudgetService.updateCheck(projectId,"design_cost_check","1");
				}

				//判断监理费是否相等
				if (sup != 0) {
					//不相等，判断是否运行少付
					if ("0".equals(supervisorUnderPayment)) {
						notPassList.add(projectId);
						projectBudgetService.updateCheck(projectId,"supervisor_cost_check","2");
					}else {
						projectBudgetService.updateCheck(projectId,"supervisor_cost_check","1");
					}
				}else{
					//验证通过
					projectBudgetService.updateCheck(projectId,"supervisor_cost_check","1");
				}
			} else {
				notExitList.add(projectId);
				projectBudgetService.updateCheck(projectId,"construction_cost_check","2");
				projectBudgetService.updateCheck(projectId,"design_cost_check","2");
				projectBudgetService.updateCheck(projectId,"supervisor_cost_check","2");
				//验证不通过
			}
		}
		List<String> strings = returnOnly(notExitList);
		List<String> strings1 = returnOnly(notPassList);
		hashMap.put("notExit", strings);
		hashMap.put("notPass", strings1);
		return hashMap;
	}

	private List<String> returnOnly(List<String> notExitList) {
		HashSet a = new HashSet(notExitList);
		notExitList.clear();
		notExitList.addAll(a);
		return notExitList;
	}
}