/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.number.NumberUtil;
import com.jeeplus.common.utils.text.TextValidator;
import com.jeeplus.common.utils.time.DateUtil;
import com.mchange.lang.IntegerUtils;
import freemarker.template.SimpleDate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sys.entity.CompletionInformation;
import com.jeeplus.modules.sys.mapper.CompletionInformationMapper;

import javax.validation.constraints.NotNull;

/**
 * ItemListService
 * @author qiuyue
 * @version 2021-12-23
 */
@Service
@Transactional(readOnly = true)
public class CompletionInformationService extends CrudService<CompletionInformationMapper, CompletionInformation> {

	public CompletionInformation get(String id) {
		return super.get(id);
	}
	
	public List<CompletionInformation> findList(CompletionInformation completionInformation) {
		return super.findList(completionInformation);
	}
	
	public Page<CompletionInformation> findPage(Page<CompletionInformation> page, CompletionInformation completionInformation) {
		return super.findPage(page, completionInformation);
	}
	
	@Transactional(readOnly = false)
	public void save(CompletionInformation completionInformation) {
		super.save(completionInformation);
	}

	@Transactional(readOnly = false)
	public void insert(CompletionInformation completionInformation) {
//		项目类型含“主业”字符，且合同金额50万以下（含50万）时限设为3天
//		项目类型含“主业”字符，且合同金额50万以上时限设为7天；
//		项目类型含“客户”字符，时限为30天。
//		不包含两种字符，时限为3天
		//项目类型
		try {
		String projectType = completionInformation.getProjectType();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String completionDate = completionInformation.getCompletionDate();
		Date parse = dateFormat.parse(completionDate);
		BigDecimal limit = new BigDecimal("500000");
		BigDecimal contractAmount = new  BigDecimal(completionInformation.getContractAmount());
		if(projectType.contains("主业")){
			if(contractAmount.compareTo(limit)<=0){
				completionInformation.setTimeLimit("3");
				String format = dateFormat.format(DateUtil.addDays(parse, 3));
				completionInformation.setExpectedDate(format);
			}else{
				completionInformation.setTimeLimit("7");
				String format = dateFormat.format(DateUtil.addDays(parse, 7));
				completionInformation.setExpectedDate(format);
			}
		}else if(projectType.contains("客户")){
			completionInformation.setTimeLimit("30");
			String format = dateFormat.format(DateUtil.addDays(parse, 30));
			completionInformation.setExpectedDate(format);
		}else {
			completionInformation.setTimeLimit("3");
			String format = dateFormat.format(DateUtil.addDays(parse, 3));
			completionInformation.setExpectedDate(format);
		}

		completionInformation.preInsert();
		mapper.insert(completionInformation);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Transactional(readOnly = false)
	public void update(CompletionInformation completionInformation) {
//		项目类型含“主业”字符，且合同金额50万以下（含50万）时限设为3天
//		项目类型含“主业”字符，且合同金额50万以上时限设为7天；
//		项目类型含“客户”字符，时限为30天。
//		不包含两种字符，时限为3天
		//项目类型
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String completionDate = completionInformation.getCompletionDate();
			Date parse = dateFormat.parse(completionDate);
			String timeLimit = completionInformation.getTimeLimit();
			String format = dateFormat.format(DateUtil.addDays(parse, Integer.valueOf(timeLimit)));
			completionInformation.setExpectedDate(format);
			completionInformation.preUpdate();
			mapper.update(completionInformation);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Transactional(readOnly = false)
	public void insertList(List<CompletionInformation> list) {
//		项目类型含“主业”字符，且合同金额50万以下（含50万）时限设为3天
//		项目类型含“主业”字符，且合同金额50万以上时限设为7天；
//		项目类型含“客户”字符，时限为30天。
//		不包含两种字符，时限为3天
		//项目类型
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			BigDecimal limit = new BigDecimal("500000");
			for (CompletionInformation completionInformation:
				 list) {
				String projectType = completionInformation.getProjectType();
				String completionDate = completionInformation.getCompletionDate();
				Date parse = dateFormat.parse(completionDate);
				BigDecimal contractAmount = new  BigDecimal(completionInformation.getContractAmount());
				if(projectType.contains("主业")){
					if(contractAmount.compareTo(limit)<=0){
						completionInformation.setTimeLimit("3");
						String format = dateFormat.format(DateUtil.addDays(parse, 3));
						completionInformation.setExpectedDate(format);
					}else{
						completionInformation.setTimeLimit("7");
						String format = dateFormat.format(DateUtil.addDays(parse, 7));
						completionInformation.setExpectedDate(format);
					}
				}else if(projectType.contains("客户")){
					completionInformation.setTimeLimit("30");
					String format = dateFormat.format(DateUtil.addDays(parse, 30));
					completionInformation.setExpectedDate(format);
				}else {
					completionInformation.setTimeLimit("3");
					String format = dateFormat.format(DateUtil.addDays(parse, 3));
					completionInformation.setExpectedDate(format);
				}
				completionInformation.preInsert();
			}
			mapper.insertList(list);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Transactional(readOnly = false)
	public void delete(CompletionInformation completionInformation) {
		super.delete(completionInformation);
	}

	@Transactional(readOnly = false)
	public  void updateDelFlag(String id){
		mapper.updateDelFlag(id);
	}

	public boolean isSubmissionDate(String[] ids){
		boolean flag = true;
		List<CompletionInformation> list = mapper.isSubmissionDate(ids);
		for (CompletionInformation com :
				list) {
			String submissionDate = com.getSubmissionDate();
			if (StringUtils.isNotBlank(submissionDate)) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	@Transactional(readOnly = false)
	public int updateSubmissionDate(String[] ids){
		List<CompletionInformation> list = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(new Date());
		for (String a :
				ids) {
			CompletionInformation com = new CompletionInformation();
			com.setId(a);
			com.setSubmissionDate(format);
			list.add(com);
		}
		return mapper.updateSubmissionDate(list);
	}

	/**
	 * 导入验证，如果有项目不符合
	 * */
	public String importVerification(List<CompletionInformation> list) throws ParseException {
		StringBuilder failureMsg = new StringBuilder();
		boolean flag = false;
		for (CompletionInformation comp :
				list) {
			String projectYear = comp.getProjectYear();
			String itemCode = comp.getItemCode();
			String projectType = comp.getProjectType();
			String entryName = comp.getEntryName();
			String constructionUnit = comp.getConstructionUnit();
			String contractAmount = comp.getContractAmount();
			String completionDate = comp.getCompletionDate();
			failureMsg.delete(0, failureMsg.length());
			failureMsg.append("工程年度：《"+projectYear+"》项目名称《"+entryName+"》项目编码《"+itemCode+"》数据有误:");
			if (StringUtils.isBlank(projectYear)){
				failureMsg.append("工程年度不能为空");
				flag = true;
				break;
			}else{
				if (!TextValidator.isYear(projectYear)) {
					failureMsg.append("工程年度请输入正常年份");
					flag = true;
					break;
				}
			}
			if (StringUtils.isBlank(itemCode)){
				failureMsg.append("项目编码不能为空");
				flag = true;
				break;
			}
			if (StringUtils.isBlank(projectType)){
				failureMsg.append("项目类型不能为空");
				flag = true;
				break;
			}
			if (StringUtils.isBlank(entryName)){
				failureMsg.append("项目名称不能为空");
				flag = true;
				break;
			}
			if (StringUtils.isBlank(constructionUnit)){
				failureMsg.append("施工单位不能为空");
				flag = true;
				break;
			}
			if(StringUtils.isBlank(contractAmount)){
				failureMsg.append("合同金额不能为空");
				flag = true;
				break;
			}else{
				if (!NumberUtil.isNumber(contractAmount)){
					failureMsg.append("请输入正确的合同金额");
					flag = true;
					break;
				}
			}
			if(StringUtils.isBlank(completionDate)){
				failureMsg.append("完工日期不能为空");
				flag = true;
				break;
			}else{
				if (!TextValidator.isDate(completionDate)) {
					failureMsg.append("请输入正确的完工日期");
					flag = true;
					break;
				}
			}
		}
		if (flag) {
			return failureMsg.toString();
		}else{
			return failureMsg.delete(0, failureMsg.length()).toString();
		}
	}


}