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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.basedata.entity.FdPassFeeClassProject;
import org.jeecg.modules.financeManage.entity.FdHandoverfee;
import org.jeecg.modules.financeManage.entity.FdHandoverfeeB;
import org.jeecg.modules.financeManage.mapper.FdHandoverfeeBMapper;
import org.jeecg.modules.financeManage.mapper.FdHandoverfeeMapper;
import org.jeecg.modules.financeManage.service.IFdHandoverfeeService;
import org.jeecg.modules.projectManagement.entity.*;
import org.jeecg.modules.projectManagement.service.IFdContractClauseService;
import org.jeecg.modules.projectManagement.service.IFdContractCompoundService;
import org.jeecg.modules.projectManagement.service.IFdProjectFileCompoundService;
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 javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 财务过手费主表
 * @author： jeecg-boot
 * @date：   2019-07-13
 * @version： V1.0
 */
@Service
public class FdHandoverfeeServiceImpl extends ServiceImpl<FdHandoverfeeMapper, FdHandoverfee> implements IFdHandoverfeeService {

	@Resource
	private FdHandoverfeeMapper fdHandoverfeeMapper;
	@Resource
	private FdHandoverfeeBMapper fdHandoverfeeBMapper;

	@Resource
	private IFdContractCompoundService iFdContractCompoundService;

	@Override
	@Transactional
	public void saveMain(FdHandoverfee fdHandoverfee, List<FdHandoverfeeB> fdHandoverfeeBList) {
		fdHandoverfeeMapper.insert(fdHandoverfee);
		for(FdHandoverfeeB entity:fdHandoverfeeBList) {
			//外键设置
			entity.setPkHeadId(fdHandoverfee.getId());
			fdHandoverfeeBMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(FdHandoverfee fdHandoverfee,List<FdHandoverfeeB> fdHandoverfeeBList) {
		fdHandoverfeeMapper.updateById(fdHandoverfee);
		
		//1.先删除子表数据
		fdHandoverfeeBMapper.deleteByMainId(fdHandoverfee.getId());
		
		//2.子表数据重新插入
		for(FdHandoverfeeB entity:fdHandoverfeeBList) {
			//外键设置
			entity.setPkHeadId(fdHandoverfee.getId());
			fdHandoverfeeBMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateAll(FdHandoverfee fdHandoverfee,List<FdHandoverfeeB> fdHandoverfeeBList) {
		fdHandoverfeeMapper.updateById(fdHandoverfee);

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

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

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

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fdHandoverfeeMapper.deleteById(id);
			fdHandoverfeeBMapper.deleteByMainId(id.toString());
		}
	}
	@Resource
	private IFdProjectFileCompoundService fdProjectFileCompoundService;
	@Resource
	private IFdContractClauseService fdContractClauseService;
	/**
	 * 通过合同号，查询合同所有关联数据
	 * @param pkProjectFile
	 * @return
	 */
	public FdProjectFile findContractByCode(String pkProjectFile){
		FdProjectFile contractByCode = fdHandoverfeeMapper.findContractByCode(pkProjectFile);
		QueryWrapper<FdContractClause> clauseQueryWrapper = new QueryWrapper<>();
		clauseQueryWrapper.eq("contract_id", contractByCode.getContractId());
		contractByCode.setFdContractClauseList(fdContractClauseService.list(clauseQueryWrapper));
//		QueryWrapper<FdProjectFileCompound> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("project_id", contractByCode.getId());
//		queryWrapper.eq("del_flag","0");
//		List<FdProjectFileCompound> list = fdProjectFileCompoundService.list(queryWrapper);
		LambdaQueryWrapper<FdContractCompound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(FdContractCompound::getDelFlag,"0");
		lambdaQueryWrapper.eq(FdContractCompound::getContractId,contractByCode.getContractId());
		lambdaQueryWrapper.orderByAsc(FdContractCompound::getSort);
		List<FdContractCompound> list=iFdContractCompoundService.list(lambdaQueryWrapper);
		String compound ="";
		Integer i = 0;
		for ( FdContractCompound fdContractCompound : list ) {
			i++;
			compound += fdContractCompound.getCompound();
			if (i<list.size()){
				compound+=",";
			}
		}
		contractByCode.setCompound(compound);
		return contractByCode;
	}

	/**
	 * 通过过手费编码，查询所有关联数据
	 * @param code
	 * @return
	 */
	public List<FdPassFeeClassProject> findHandoverfeeByCode(String code){
		return fdHandoverfeeMapper.findHandoverfeeByCode(code);
	}

	/**
	 * 通过合同编码，查询收款条件
	 * @param code
	 * @return
	 */
	public List<FdHandoverfeeB> selectClause(String code){
		return fdHandoverfeeMapper.selectClause(code);
	}

	/**
	 * 查询合同下拉
	 * @return
	 */
	public List<FdHandoverfee> selectContract(){
		return fdHandoverfeeMapper.selectContract();
	}
	/**
	 * 查询重复过手费号码
	 * @param code
	 * @return
	 */
	public int fingMoreCode(String code,String id){
		return fdHandoverfeeMapper.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){
			fdHandoverfeeMapper.toExamine(state,li);
		}
	}
}
