/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-06-10
 * 描述            ：成本分配依据表-主
 * 注意事项     ：
 * 遗留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.BaseCostCptMService;
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.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="BaseCostCptMService")
public class BaseCostCptMServiceImpl extends BaseServiceImpl<BaseCostCptM,Object> implements BaseCostCptMService {

	/**
	 * 查询成本核算规则主表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCostCptM(CommonPage<BaseCostCptM> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostCptM bcc = cp.getObject();
		try {
			String swhere = "";
			if (bcc.getDst_item_name()!=null && !"".equals(bcc.getDst_item_name())) {
				swhere+=" and t.dst_item_name like '%"+bcc.getDst_item_name()+"%'";//分配项目名称
			}
			if (bcc.getDst_basis_code()!=null && !"".equals(bcc.getDst_basis_code())) {
				swhere+=" and t.dst_basis_code = '"+bcc.getDst_basis_code()+"'";//分配依据代码
			}
			if (bcc.getItem_name()!=null && !"".equals(bcc.getItem_name())) {
				swhere+=" and bdi.item_name like '%"+bcc.getItem_name()+"%'";//成本项目名称
			}
			if (bcc.getItem_code()!=null && !"".equals(bcc.getItem_code())) {
				swhere+=" and bdi.item_code = '"+bcc.getItem_code()+"'";//成本项目代码
			}
			if (bcc.getValid_flg()!=null && !"".equals(bcc.getValid_flg())) {
				swhere+=" and t.valid_flg = '"+bcc.getValid_flg()+"'"; //是否有效
			}
			List<BaseCostCptM> list = this.getMapper(BaseCostCptMMapper.class).doQueryBaseCostCptM(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 成本核算管理界面：查询成本核算规则主表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCostCptMForResult(CommonPage<BaseCostCptM> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostCptM bcc = cp.getObject();
		try {
			String swhere = "";
			if (bcc.getDst_item_name()!=null && !"".equals(bcc.getDst_item_name())) {
				swhere+=" and t.dst_item_name like '%"+bcc.getDst_item_name()+"%'";//分配项目名称
			}
			if (bcc.getDst_basis_code()!=null && !"".equals(bcc.getDst_basis_code())) {
				swhere+=" and t.dst_basis_code = '"+bcc.getDst_basis_code()+"'";//分配依据代码
			}
			if (bcc.getItem_name()!=null && !"".equals(bcc.getItem_name())) {
				swhere+=" and t.item_name like '%"+bcc.getItem_name()+"%'";//成本项目名称
			}
			if (bcc.getItem_code()!=null && !"".equals(bcc.getItem_code())) {
				swhere+=" and t.item_code = '"+bcc.getItem_code()+"'";//成本项目代码
			}
			if (bcc.getValid_flg()!=null && !"".equals(bcc.getValid_flg())) {
				swhere+=" and t.valid_flg = '"+bcc.getValid_flg()+"'"; //是否有效
			}
			List<BaseCostCptM> list = this.getMapper(BaseCostCptMMapper.class).doQueryBaseCostCptMForResult(swhere,cp.getMemo1());
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	
	/**
	 * 新增成本分配规则主表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertBaseCostCptM(CommonPage<BaseCostCptM> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm  = new ResultModel();
		BaseCostCptM bcc = cp.getObject();
		if (bcc.getDst_item_code()==null || "".equals(bcc.getDst_item_code())) {
			throw new Exception("分配项目代码不能为空");
		}
		if (bcc.getCpt_index()==null || "".equals(bcc.getCpt_index())) {
			throw new Exception("核算顺序不能为空");
		}
		if (bcc.getCpt_type()==null || "".equals(bcc.getCpt_type())) {
			throw new Exception("核算类型不能为空");
		}
		BaseDstItem bdi = this.getMapper(BaseDstItemMapper.class).findById(bcc.getDst_item_code());
		if (bdi==null) {
			throw new Exception("选择的分配项目不存在");
		}
		if ("0".equals(bdi.getValid_flg())) {
			throw new Exception("选择的分配项目已经无效");
		}
		//判断分配项目是否重复
		BaseCostCptM BCC = this.getMapper(BaseCostCptMMapper.class).findById(bcc.getDst_item_code());
		if (BCC!=null) {
			throw new Exception("分配目标项目里已经存在相同的分配项目，无法新增");
		}
		bcc.setParent_item_code(bdi.getParent_item_code());
		bcc.setParent_item_name(bdi.getParent_item_name());
		bcc.setItem_code(bdi.getItem_code());
		bcc.setItem_name(bdi.getItem_name());
		bcc.setFormula_sign(bdi.getFormula_sign()); //公式符号
		bcc.setMsr_unit(bdi.getMsr_unit());			//计量单位
		bcc.setUnit_prc(bdi.getUnit_prc());			//单位精度		
		//bcc.setDst_basis_code(dst_basis_code);//分配依据
		//bcc.setDst_basis_name(dst_basis_name);
		//bcc.setCpt_index(cpt_index);
		//bcc.setCpt_type(cpt_type);
		bcc.setCreate_man(userName);
		bcc.setCreate_time(new Date());
		this.getMapper(BaseCostCptMMapper.class).insert(bcc);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 删除成分分配规则主表
	 * @param cp dst_item_code分配项目代码
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseCostCptM(CommonPage<BaseCostCptM> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostCptM bcc = cp.getObject();
		if (bcc.getDst_item_code()==null || "".equals(bcc.getDst_item_code())) {
			throw new Exception("分配项目代码不能为空");
		}
		BaseCostCptD bccd = new BaseCostCptD();
		bccd.setDst_item_code_f(bcc.getDst_item_code());
		int count = this.getMapper(BaseCostCptDMapper.class).count(bccd);
		if (count > 0) {
			throw new Exception("当前分配规则下存在具体的分配规则信息，无法删除");
		}
		this.getMapper(BaseCostCptMMapper.class).delete(bcc.getDst_item_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 保存公式
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doUpdateBaseCostCptM(CommonPage<BaseCostCptM> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm  = new ResultModel();
		BaseCostCptM bcc = cp.getObject();
		if (bcc.getDst_item_code()==null || "".equals(bcc.getDst_item_code())) {
			throw new Exception("分配目标代码不能为空");
		}
		//判断传递的分配目标项目是否存在
		BaseCostCptM BCC = this.getMapper(BaseCostCptMMapper.class).findById(bcc.getDst_item_code());
		if (BCC==null) {
			throw new Exception("当前选择的分配目标在数据库中不存在，请重新查询");
		}
		BaseCostCptM bccm = new BaseCostCptM();
		bccm.setDst_item_code(bcc.getDst_item_code());
		bccm.setFormula_amount(bcc.getFormula_amount());
		bccm.setFormula_dsc(bcc.getFormula_dsc());//公式说明
		bccm.setMemo(bcc.getMemo());//备注
		bccm.setUpdate_man(userName);
		bccm.setUpdate_time(new Date());
		this.getMapper(BaseCostCptMMapper.class).update(bccm);
		rm.setState("200");
		rm.setMsgInfo("修改成功");
		return rm;
	}
	
}
