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

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.entity.WorkClientBank;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
import com.jeeplus.modules.workcontractinfo.dao.WorkContractInfoDao;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractInfo;
import com.jeeplus.modules.workmonthlyplan.dao.WorkMonthlyPlanThisDao;
import com.jeeplus.modules.workmonthlyplan.entity.WorkMonthlyPlanThis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workmonthlyplan.entity.WorkMonthlyPlan;
import com.jeeplus.modules.workmonthlyplan.dao.WorkMonthlyPlanDao;

/**
 * 月计划管理Service
 * @author cyp
 * @version 2018-06-27
 */
@Service
@Transactional(readOnly = true)
public class WorkMonthlyPlanService extends CrudService<WorkMonthlyPlanDao, WorkMonthlyPlan> {
    @Autowired
    private WorkMonthlyPlanThisDao workMonthlyPlanThisDao;

	@Autowired
	private WorkMonthlyPlanDao workMonthlyPlanDao;
	@Autowired
	private WorkContractInfoDao workContractInfoDao;
	public WorkMonthlyPlan get(String id) {
		return super.get(id);
	}

	/**
	 * 获取查询列表
	 * @param workMonthlyPlan
	 * @return
	 */
	public List<WorkMonthlyPlan> findList(WorkMonthlyPlan workMonthlyPlan) {
		return super.findList(workMonthlyPlan);
	}
	/**
	 * 根据业务权限分页查询
	 * @param workMonthlyPlan
	 * @return
	 */
	public Page<WorkMonthlyPlan> findPage(Page<WorkMonthlyPlan> page, WorkMonthlyPlan workMonthlyPlan) {
		workMonthlyPlan.getSqlMap().put("dsf", dataScopeFilter(workMonthlyPlan.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_MONTHLYPLAN.getValue()));
		int count = dao.queryCount(workMonthlyPlan);
		page.setCount(count);
		page.setCountFlag(false);
		workMonthlyPlan.setPage(page);
		page.setList(findList(workMonthlyPlan));
		return page;
	}
	/**
	 * 保存
	 * @param workMonthlyPlan
	 * @return
	 */
	@Transactional(readOnly = false)
	public void save(WorkMonthlyPlan workMonthlyPlan) {
		super.save(workMonthlyPlan);
		//保存明细
        updateWorkMonthlyPlanInfo(workMonthlyPlan);
	}
	/**
	 * 删除
	 * @param workMonthlyPlan
	 * @return
	 */
	@Transactional(readOnly = false)
	public void delete(WorkMonthlyPlan workMonthlyPlan) {
		super.delete(workMonthlyPlan);
		deleteWorkMonthlyPlanThis(workMonthlyPlan);
	}
	/**
	 * 保存子表
	 * @param workMonthlyPlan
	 * @return
	 */
    @Transactional(readOnly = false)
    public void updateWorkMonthlyPlanInfo(WorkMonthlyPlan workMonthlyPlan) {
        for (WorkMonthlyPlanThis workMonthlyPlanThis : workMonthlyPlan.getWorkMonthlyPlanThisList()) {
			if (StringUtils.isBlank(workMonthlyPlanThis.getId()) ) {
				workMonthlyPlanThis.preInsert();
				workMonthlyPlanThis.setWorkMonthlyPlanId(workMonthlyPlan.getId());
				if(StringUtils.isNotBlank(workMonthlyPlanThis.getWorkClientInfoId())){

                    workMonthlyPlanThisDao.insert(workMonthlyPlanThis);
                }

		} else {
				workMonthlyPlanThis.preUpdate();
				workMonthlyPlanThis.setWorkMonthlyPlanId(workMonthlyPlan.getId());
                if(StringUtils.isNotBlank(workMonthlyPlanThis.getWorkClientInfoId())){
                    workMonthlyPlanThisDao.update(workMonthlyPlanThis);
                }

		}
        }
    }
    //查询子表数据
	public List<WorkMonthlyPlanThis> findWorkMonthlyPlanThisList(String id) {
		List<WorkMonthlyPlanThis> workMonthlyPlanThisList=workMonthlyPlanThisDao.findByWorkMonthlyPlanId(id);
		return workMonthlyPlanThisList;
	}

//查询本部门最近12个月计划
	public List<WorkMonthlyPlan> findHisList() {
		List<WorkMonthlyPlan> workMonthlyPlanList=workMonthlyPlanDao.findHisList(UserUtils.getSelectOffice().getId());
		return workMonthlyPlanList;
	}

	//删除子表数据
	public void deleteWorkMonthlyPlanThis(WorkMonthlyPlan workMonthlyPlan) {
		workMonthlyPlanDao.deleteWorkMonthlyPlanThis(workMonthlyPlan);

	}

	//查询引用数据
	public List<WorkMonthlyPlanThis> findWorkMonthlyPlanThis(String id) {
		List<WorkMonthlyPlanThis> workMonthlyPlanThisList=workMonthlyPlanThisDao.findByWorkMonthlyPlanThis(id);
		return workMonthlyPlanThisList;
	}

	//查询本部门最大月份
	public Date getThisMaxMonthly() throws ParseException {
		WorkMonthlyPlan workMonthlyPlan=workMonthlyPlanDao.getThisMaxMonthly(UserUtils.getSelectOffice().getId());
		if(workMonthlyPlan!=null){
			return workMonthlyPlan.getPlanMonthly();
		}else{
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date myDate = dateFormat.parse("2000-01-01");
			return myDate;
		}

	}

//验证本月是否已制定计划
	public boolean getMonthly(WorkMonthlyPlan workMonthlyPlan) {
		List<WorkMonthlyPlan> workMonthlyPlan1=workMonthlyPlanDao.getMonthly(workMonthlyPlan);
		if (workMonthlyPlan1!=null&&workMonthlyPlan1.size()>0){
			return true;
		}else {
			return false;
		}
	}


	/**
	 * 获取一个客户信息
	 * @param page 分页对象
	 * @param workClientInfo
	 * @return
	 */
	public Page<WorkClientInfo> clientInfoList(Page<WorkClientInfo> page, WorkClientInfo workClientInfo) {

		workClientInfo.setPage(page);
		List<WorkClientInfo> list = workMonthlyPlanDao.clientInfoList();
		page.setList(list);
		return page;
	}
	/**
	 * 获取所选客户本月前已收款
	 *
	 * @return
	 */
	public WorkMonthlyPlanThis getBeforeThisMonthMoney(String clientId,String planMonthly) throws ParseException {
		StringBuilder sb=new StringBuilder();
		sb.append(planMonthly);
		sb.append("-01");
		WorkMonthlyPlanThis w =new WorkMonthlyPlanThis();
		w.setContractId(clientId);
		w.setPlanMonthly(sb.toString());
		WorkMonthlyPlanThis workMonthlyPlanThis = workMonthlyPlanDao.getBeforeThisMonthMoney(w);
		return workMonthlyPlanThis;
	}
	/**
	 * 获取一个客户信息
	 * @param page 分页对象
	 * @param
	 * @return
	 */
	public Page<WorkContractInfo> findWorkContractInfoPage(Page<WorkContractInfo> page, WorkContractInfo workContractInfo) {
		workContractInfo.setPage(page);
		List<WorkContractInfo> list = workContractInfoDao.findWorkContractInfoPage(workContractInfo);
		page.setList(list);
		return page;
	}


}