/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-06-06
 * 描述            ：分配项目表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseCostCptDMapper;
import market.Cm.mapper.BaseCostCptMMapper;
import market.Cm.mapper.BaseDstItemMapper;
import market.Cm.service.BaseDstItemService;
import market.Cm.vo.BaseCostCptD;
import market.Cm.vo.BaseCostCptM;
import market.Cm.vo.BaseDstItem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="BaseDstItemService")
public class BaseDstItemServiceImpl extends BaseServiceImpl<BaseDstItem,Object> implements BaseDstItemService {
	
	/**
	 * 查询分配项目 (分页)
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseDstItem(CommonPage<BaseDstItem> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseDstItem bdi = cp.getObject();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<BaseDstItem> pm = new PageModel<BaseDstItem>(pageIndex,pageSize);
		try {
			String swhere = "";			
			if (bdi.getDst_item_name()!=null && !"".equals(bdi.getDst_item_name())) {
				swhere+=" and dst_item_name like  '%"+bdi.getDst_item_name()+"%'";//分配项目名称
			}
			if (bdi.getItem_name()!=null && !"".equals(bdi.getItem_name())) {
				swhere+=" and item_name = '"+bdi.getItem_name()+"'";//项目名称
			}
			if (bdi.getOrg_name()!=null && !"".equals(bdi.getOrg_name())) {
				swhere+=" and org_name like  '%"+bdi.getOrg_name()+"%'";//组织名称
			}			
			if ("1".equals(bdi.getValid_flg())) {
				swhere+=" and valid_flg = '1'"; //是否有效
			}
			pm.setRows(this.getMapper(BaseDstItemMapper.class).findWithCondition(null, swhere, "index_id,formula_sign", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(BaseDstItemMapper.class).countWithCondition(null, swhere));			
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 查询分配项目 (不分页)
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryDstItemNotPage(CommonPage<BaseDstItem> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseDstItem bdi = cp.getObject();
		try {
			String swhere = "";			
			if (bdi.getDst_item_name()!=null && !"".equals(bdi.getDst_item_name())) {
				swhere+=" and dst_item_name like '%"+bdi.getDst_item_name()+"%'";//分配项目名称
			}
			if (bdi.getItem_name()!=null && !"".equals(bdi.getItem_name())) {
				swhere+=" and item_name = '"+bdi.getItem_name()+"'";//项目名称
			}
			if (bdi.getItem_code()!=null && !"".equals(bdi.getItem_code())) {
				swhere+=" and item_code = '"+bdi.getItem_code()+"'";//项目编码
			}
			if (bdi.getOrg_name()!=null && !"".equals(bdi.getOrg_name())) {
				swhere+=" and org_name like '%"+bdi.getOrg_name()+"%'";//组织名称
			}
			if (bdi.getOrg_code()!=null && !"".equals(bdi.getOrg_code())) {
				swhere+=" and org_code = '"+bdi.getOrg_code()+"'";//组织代码
			}
			if (bdi.getIs_dst()!=null && !"".equals(bdi.getIs_dst())) {
				swhere+=" and is_dst = '"+bdi.getIs_dst()+"'";//是否分配(1分配目标、0分配依据)
			}
			if (bdi.getValid_flg()!=null && !"".equals(bdi.getValid_flg())) {
				swhere+=" and valid_flg = '"+bdi.getValid_flg()+"'";//是否有效
			}
			if (bdi.getTable_name()!=null && !"".equals(bdi.getTable_name())) {
				swhere+=" and table_name = '"+bdi.getTable_name()+"'";//数据表来源
			}
			if (bdi.getIs_result()!=null && !"".equals(bdi.getIs_result())) {
				swhere+=" and is_result = '"+bdi.getIs_result()+"'";//是否结果
			}
			if (bdi.getData_origin()!=null && !"".equals(bdi.getData_origin())) {
				swhere+=" and data_origin = '"+bdi.getData_origin()+"'";//数据来源
			}
			List<BaseDstItem> list = this.getMapper(BaseDstItemMapper.class).findWithCondition(null, swhere, "dst_item_name", 0, 0);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 新增分配项目
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertBaseDstItem(CommonPage<BaseDstItem> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm  = new ResultModel();
		BaseDstItem bdi = cp.getObject();
		//必填项验证
		this.doCheckBaseDstItem(bdi);
		//分配项目名称重复验证
		BaseDstItem BDI = new BaseDstItem();
		BDI.setDst_item_name(bdi.getDst_item_name());
		int count = this.getMapper(BaseDstItemMapper.class).count(BDI);
		if (count > 0) {
			throw new Exception("存在相同名称的分配项目名称");
		}
		//公式符号重复验证
		BDI = new BaseDstItem();
		BDI.setFormula_sign(bdi.getFormula_sign());
		count = this.getMapper(BaseDstItemMapper.class).count(BDI);
		if (count > 0) {
			throw new Exception("存在相同公式符号");
		}
		//生成分配项目代码
		List<BaseDstItem> list = this.getMapper(BaseDstItemMapper.class).findWithCondition(null, null, "dst_item_code desc", 0, 0);
		if (list==null || list.size()<=0) {
			bdi.setDst_item_code("DS001");
		}else {
			String maxCode = list.get(0).getDst_item_code();
			String seq = maxCode.substring(maxCode.length()-3);
			int iseq = Integer.parseInt(seq) + 1;
			bdi.setDst_item_code("DS" + String.format("%03d", iseq));
		}
		bdi.setValid_flg("1");
		bdi.setCreate_man(userName);
		bdi.setCreate_time(new Date());
		this.getMapper(BaseDstItemMapper.class).insert(bdi);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 修改分配项目
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doUpdateBaseDstItem(CommonPage<BaseDstItem> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		BaseDstItem bdi = cp.getObject();
		//必填项验证
		this.doCheckBaseDstItem(bdi);
		
		BaseDstItem BDI = this.getMapper(BaseDstItemMapper.class).findById(bdi.getDst_item_code());
		if (BDI==null) {
			throw new Exception("当前选择行的分配项目不存在，请重新查询");
		}
		if ("0".equals(BDI.getValid_flg())) {
			throw new Exception("分配项目【"+BDI.getDst_item_name()+"】已经禁用，无法修改");
		}
		
		//判断名称是否重复
		String condition = "dst_item_code <> '"+bdi.getDst_item_code()+"' and dst_item_name = '"+bdi.getDst_item_name()+"' ";
		List<BaseDstItem> list = this.getMapper(BaseDstItemMapper.class).findWithCondition(null, condition, null, 0, 0);
		if (list!=null && list.size()>0) {
			throw new Exception("存在相同的分配项目，无法修改");
		}
		//判断公式符号是否重复
		condition = "dst_item_code <> '"+bdi.getDst_item_code()+"' and formula_sign = '"+bdi.getFormula_sign()+"' ";
		list = this.getMapper(BaseDstItemMapper.class).findWithCondition(null, condition, null, 0, 0);
		if (list!=null && list.size()>0) {
			throw new Exception("存在相同的公式符号，无法修改");
		}
		bdi.setUpdate_man(userName);
		bdi.setUpdate_time(new Date());
		this.getMapper(BaseDstItemMapper.class).update(bdi);
		
		//如果公式符号进行了修改，同步修改核算规则主从表的公式
		if (!bdi.getFormula_sign().equals(BDI.getFormula_sign())) {
			//主表
			BaseCostCptM bccM = this.getMapper(BaseCostCptMMapper.class).findById(bdi.getDst_item_code());
			if (bccM!=null) {
				//替换公式符号
				bccM.setFormula_sign(bdi.getFormula_sign());
				if (bccM.getFormula_amount()!=null && bccM.getFormula_amount().contains(BDI.getFormula_sign())) {
					//替换核算公式
					bccM.setFormula_amount(bccM.getFormula_amount().replace(BDI.getFormula_sign(),bdi.getFormula_sign()));
				}
				this.getMapper(BaseCostCptMMapper.class).update(bccM);
			}
			//从表
			String swhere = " and (dst_item_code = '"+bdi.getDst_item_code()+"' or dst_item_code_f = '"+bdi.getDst_item_code()+"')";
			List<BaseCostCptD> list2 = this.getMapper(BaseCostCptDMapper.class).findWithCondition(null, swhere, null, 0, 0);
			if (list2!=null && list2.size()>0) {
				for (BaseCostCptD bccD : list2) {
					bccD.setFormula_sign(bdi.getFormula_sign());
					//替换分配比率公式
					if (bccD.getFormula_rate()!=null && bccD.getFormula_rate().contains(BDI.getFormula_sign())) {
						bccD.setFormula_rate(bccD.getFormula_rate().replace(BDI.getFormula_sign(),bdi.getFormula_sign()));
					}
					//替换核算公式
					if (bccD.getFormula_amount()!=null && bccD.getFormula_amount().contains(BDI.getFormula_sign())) {
						bccD.setFormula_amount(bccD.getFormula_amount().replace(BDI.getFormula_sign(),bdi.getFormula_sign()));
					}
					this.getMapper(BaseCostCptDMapper.class).update(bccD);
				}
			}
		}
		
		rm.setState("200");
		rm.setMsgInfo("修改成功");
		return rm;
	}
	
	/**
	 * 删除分配项目
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseDstItem(CommonPage<BaseDstItem> cp) throws Exception{
		ResultModel rm = new ResultModel();
		BaseDstItem bdi = cp.getObject();
		if (bdi.getDst_item_code()==null || "".equals(bdi.getDst_item_code())) {
			throw new Exception("请传入分配项目代码");
		}		 
		this.getMapper(BaseDstItemMapper.class).delete(bdi.getDst_item_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 启用/禁止 分配项目
	 * @param cp memo1[0:禁用    1:启用]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeBaseDstItemValidFlg(CommonPage<BaseDstItem> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		String reslut = null;
		BaseDstItem bdi = cp.getObject();
		if (bdi.getDst_item_code()==null || "".equals(bdi.getDst_item_code())) {
			throw new Exception("请传入分配项目代码");
		}
		bdi.setUpdate_man(userName);
		bdi.setUpdate_time(new Date());
		if ("1".equals(opr)) {//启用
			bdi.setValid_flg("1");
			reslut = "启用";
		}else if ("0".equals(opr)) { //禁用
			bdi.setValid_flg("0");
			reslut = "禁用";
		}
		this.getMapper(BaseDstItemMapper.class).update(bdi);
		rm.setState("200");
		rm.setMsgInfo(reslut + "操作成功");
		return rm;
	}
	
	/**
	 * 新增修改必填项验证
	 * @param bdi
	 * @throws Exception
	 */
	public void doCheckBaseDstItem(BaseDstItem bdi) throws Exception{
		if (bdi.getDst_item_name()==null || "".equals(bdi.getDst_item_name())) {
			throw new Exception("分配项目名称不能为空");
		}
		/*if (bdi.getParent_item_code()==null || "".equals(bdi.getParent_item_code())) {
			throw new Exception("项目父代码不能为空");
		}
		if (bdi.getParent_item_name()==null || "".equals(bdi.getParent_item_name())) {
			throw new Exception("项目父名称不能为空");
		}
		if (bdi.getItem_code()==null || "".equals(bdi.getItem_code())) {
			throw new Exception("项目代码不能为空");
		}
		if (bdi.getItem_name()==null || "".equals(bdi.getItem_name())) {
			throw new Exception("项目名称不能为空");
		}*/
		if (bdi.getData_origin()==null || "".equals(bdi.getData_origin())) {
			throw new Exception("数据来源不能为空");
		}
		if (bdi.getFormula_sign()==null || "".equals(bdi.getFormula_sign())) {
			throw new Exception("公式符号不能为空");
		}
	}
	
}
