package org.jeecg.modules.financeManage.service.impl;

import org.apache.commons.codec.binary.Base64;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.financeManage.entity.FdProjectReceivables;
import org.jeecg.modules.financeManage.entity.FdProjectReceivablesB;
import org.jeecg.modules.financeManage.mapper.FdProjectReceivablesBMapper;
import org.jeecg.modules.financeManage.mapper.FdProjectReceivablesMapper;
import org.jeecg.modules.financeManage.service.IFdProjectReceivablesService;
import org.jeecg.modules.message.handle.ISendMsgHandle;
import org.jeecg.modules.message.handle.enums.SendMsgTypeEnum;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目应收款主表
 * @author： jeecg-boot
 * @date：   2019-07-22
 * @version： V1.0
 */
@Service
public class FdProjectReceivablesServiceImpl extends ServiceImpl<FdProjectReceivablesMapper, FdProjectReceivables> implements IFdProjectReceivablesService {

	@Autowired
	private FdProjectReceivablesMapper fdProjectReceivablesMapper;
	@Autowired
	private FdProjectReceivablesBMapper fdProjectReceivablesBMapper;
	@Resource
	IFdPersonnelService fdPersonnelService;
	
	@Override
	@Transactional
	public void saveMain(FdProjectReceivables fdProjectReceivables, List<FdProjectReceivablesB> fdProjectReceivablesBList) {
		fdProjectReceivablesMapper.insert(fdProjectReceivables);
		for(FdProjectReceivablesB entity:fdProjectReceivablesBList) {
			//外键设置
			entity.setPkHeadId(fdProjectReceivables.getId());
			fdProjectReceivablesBMapper.insert(entity);
		}
		// fdProjectReceivablesMapper.updateHeadNum(fdProjectReceivables.getId());
	}

	@Override
	@Transactional
	public void updateMain(FdProjectReceivables fdProjectReceivables,List<FdProjectReceivablesB> fdProjectReceivablesBList) {
		fdProjectReceivablesMapper.updateById(fdProjectReceivables);
		
		//1.先删除子表数据
		fdProjectReceivablesBMapper.deleteByMainId(fdProjectReceivables.getId());
		
		//2.子表数据重新插入
		for(FdProjectReceivablesB entity:fdProjectReceivablesBList) {
			//外键设置
			entity.setPkHeadId(fdProjectReceivables.getId());
			fdProjectReceivablesBMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateAll(FdProjectReceivables fdHandoverfee,List<FdProjectReceivablesB> fdHandoverfeeBList) {
		fdProjectReceivablesMapper.updateById(fdHandoverfee);

		//1.先删除子表数据
		fdProjectReceivablesBMapper.deleteById(fdHandoverfee.getId());

		//2.子表数据重新插入
		for(FdProjectReceivablesB entity:fdHandoverfeeBList) {
			//外键设置
			entity.setPkHeadId(fdHandoverfee.getId());
			fdProjectReceivablesBMapper.insert(entity);
		}
		fdProjectReceivablesMapper.updateHeadNum(fdHandoverfee.getId());
	}


	@Override
	@Transactional
	public void delMain(String id) {
		fdProjectReceivablesMapper.deleteById(id);
		fdProjectReceivablesBMapper.deleteByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fdProjectReceivablesMapper.deleteById(id);
			fdProjectReceivablesBMapper.deleteByMainId(id.toString());
		}
	}

	/**
	 * 查询重复过手费号码
	 * @param code
	 * @return
	 */
	public int fingMoreCode(String code,String id){
		return fdProjectReceivablesMapper.fingMoreCode(code,id);
	}

	/**
	 * 审核
	 * @param ids
	 * @param state
	 */
	@Transactional
	public void toExamine(String ids,String state) {
		List<String> list = Arrays.asList(ids.split(","));
		for (String li:list){
			fdProjectReceivablesMapper.toExamine(state,li);
		}
	}

	/***
	 * @Title:邮件通知财务收费
	        * @Description:TODO
	        * @param msg
	        * @return void
	        * @exception
	        * @author 陈创
	        * @date 2019/8/13 19:50
	        * @updatehistory:
	 *   2019/8/13 19:50 通知
	        */
	@Override
	public void noticeFinance(String personnel,String title,String msg) {
		//根据人id查询邮件地址
		FdPersonnel fdPersonnel=fdPersonnelService.getById(personnel);
		ISendMsgHandle sendMsgHandle = null;
		try {
		    sendMsgHandle = (ISendMsgHandle) Class.forName(SendMsgTypeEnum.EMAIL.getImplClass()).newInstance();
		} catch (Exception e) {
		    e.printStackTrace();
		}
		//发起邮件通知
		try {

			// SecretKey deskey = new SecretKeySpec("email".getBytes(), "DESede");
			// Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// try {
			// 	c1.init(Cipher.DECRYPT_MODE, deskey);
			// } catch (InvalidKeyException e) {
			// 	e.printStackTrace();
			// }
			// byte[] result = c1.doFinal(Base64.decodeBase64(fdPersonnel.getEmail()));
			// System.out.println(new String(result, "UTF-8"));

			// BASE64Decoder decoder = new BASE64Decoder();
			// byte bytesrc[] = decoder.decodeBuffer("U2FsdGVkX1/0gq3wOL4PsRN7Qnuor03C");
			// DESedeKeySpec dks = new DESedeKeySpec("aibizhall-biz-zzyyt-1234".getBytes("UTF-8"));
			// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			// javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// cipher.init(2, securekey);
			// byte retByte[] = cipher.doFinal(bytesrc);
			// String ss=new String(retByte);

			sendMsgHandle.SendMsg(fdPersonnel.getEmail(),title, msg);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new RuntimeException("检查通知人员的邮箱是否有误");
		}
	}

	@Override
	public void noticeFinance2(String personnelEmail,String title,String msg) {
		//根据人id查询邮件地址
//		FdPersonnel fdPersonnel=fdPersonnelService.getById(personnel);
		ISendMsgHandle sendMsgHandle = null;
		try {
			sendMsgHandle = (ISendMsgHandle) Class.forName(SendMsgTypeEnum.EMAIL.getImplClass()).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//发起邮件通知
		try {

			// SecretKey deskey = new SecretKeySpec("email".getBytes(), "DESede");
			// Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// try {
			// 	c1.init(Cipher.DECRYPT_MODE, deskey);
			// } catch (InvalidKeyException e) {
			// 	e.printStackTrace();
			// }
			// byte[] result = c1.doFinal(Base64.decodeBase64(fdPersonnel.getEmail()));
			// System.out.println(new String(result, "UTF-8"));

			// BASE64Decoder decoder = new BASE64Decoder();
			// byte bytesrc[] = decoder.decodeBuffer("U2FsdGVkX1/0gq3wOL4PsRN7Qnuor03C");
			// DESedeKeySpec dks = new DESedeKeySpec("aibizhall-biz-zzyyt-1234".getBytes("UTF-8"));
			// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			// javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// cipher.init(2, securekey);
			// byte retByte[] = cipher.doFinal(bytesrc);
			// String ss=new String(retByte);

			sendMsgHandle.SendMsg(personnelEmail,title, msg);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new RuntimeException("检查通知人员的邮箱是否有误");
		}
	}



	@Override
	public void peopleSendMsg(String senderEmail,String personnel,String title,String msg) {
		//根据人id查询邮件地址
		FdPersonnel fdPersonnel=fdPersonnelService.getById(personnel);
		ISendMsgHandle sendMsgHandle = null;
		try {
			sendMsgHandle = (ISendMsgHandle) Class.forName(SendMsgTypeEnum.EMAIL.getImplClass()).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//发起邮件通知
		try {

			// SecretKey deskey = new SecretKeySpec("email".getBytes(), "DESede");
			// Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// try {
			// 	c1.init(Cipher.DECRYPT_MODE, deskey);
			// } catch (InvalidKeyException e) {
			// 	e.printStackTrace();
			// }
			// byte[] result = c1.doFinal(Base64.decodeBase64(fdPersonnel.getEmail()));
			// System.out.println(new String(result, "UTF-8"));

			// BASE64Decoder decoder = new BASE64Decoder();
			// byte bytesrc[] = decoder.decodeBuffer("U2FsdGVkX1/0gq3wOL4PsRN7Qnuor03C");
			// DESedeKeySpec dks = new DESedeKeySpec("aibizhall-biz-zzyyt-1234".getBytes("UTF-8"));
			// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			// javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			// cipher.init(2, securekey);
			// byte retByte[] = cipher.doFinal(bytesrc);
			// String ss=new String(retByte);

			sendMsgHandle.peopleSendMsg(senderEmail,fdPersonnel.getEmail(),title, msg);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new RuntimeException("检查通知人员的邮箱是否有误");
		}
	}


	@Override
	public List<FdProjectReceivables> getListAndCreditPeriod() {
		return fdProjectReceivablesMapper.getListAndCreditPeriod();
	}

}
