/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.BaseDstItemMapper;
import market.Cm.service.BaseCostCptDService;
import market.Cm.vo.BaseCostCptD;
import market.Cm.vo.BaseDstItem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="BaseCostCptDService")
public class BaseCostCptDServiceImpl extends BaseServiceImpl<BaseCostCptD,Object> implements BaseCostCptDService {
	
	/**
	 * 查询成本分配规则从表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCostCptD(CommonPage<BaseCostCptD> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostCptD bcc = cp.getObject();
		try {
			String swhere = "";
			if (bcc.getDst_item_code_f()!=null && !"".equals(bcc.getDst_item_code_f())) {
				swhere+=" and t.dst_item_code_f = '"+bcc.getDst_item_code_f()+"'";//父层分配项目
			}					
			if (bcc.getValid_flg()!=null && !"".equals(bcc.getValid_flg())) {
				swhere+=" and t.valid_flg = '"+bcc.getValid_flg()+"'"; //是否有效
			}
			List<BaseCostCptD> list = this.getMapper(BaseCostCptDMapper.class).doQueryBaseCostCptD(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	
	/**
	 * 新增成本分配规则从表
	 * @param cp memo1分配项目代码父  memo2公式说明  memo3备注
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertBaseCostCptD(CommonPage<BaseCostCptD> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm  = new ResultModel();
		List<BaseCostCptD> bccdList = cp.getObjectList();
		if (bccdList==null || bccdList.size()<=0) {
			throw new Exception("请传入分配依据项目");
		}
		String dst_item_code_f = cp.getMemo1();	//分配目标
		String formula_dsc = cp.getMemo2();		//公式说明
		String memo = cp.getMemo3();			//备注
		Date now = new Date();
		for (BaseCostCptD bccd : bccdList) {
			bccd.setDst_item_code_f(dst_item_code_f);
			BaseDstItem bdi = this.getMapper(BaseDstItemMapper.class).findById(bccd.getDst_item_code());
			if (bdi!=null) {
				bccd.setDst_item_code(bdi.getDst_item_code());
				bccd.setDst_item_name(bdi.getDst_item_name());
				bccd.setItem_code(bdi.getItem_code());
				bccd.setItem_name(bdi.getItem_name());
				bccd.setOrg_code(bdi.getOrg_code());
				bccd.setOrg_name(bdi.getOrg_name());
				bccd.setFormula_sign(bdi.getFormula_sign());				
				bccd.setCfc_cpt(bdi.getCfc_cpt());
				bccd.setData_origin(bdi.getData_origin());
				bccd.setDefault_type(bdi.getDefault_type());
				bccd.setDefault_value(bdi.getDefault_value());
				bccd.setTable_name(bdi.getTable_name());
				bccd.setTable_comment(bdi.getTable_comment());
				bccd.setColumn_name(bdi.getColumn_name());
				bccd.setColumn_comment(bdi.getColumn_comment());
			}
			bccd.setFormula_dsc(formula_dsc);
			bccd.setMemo(memo);
			bccd.setDst_item_code_f(dst_item_code_f);
			bccd.setCreate_man(userName);
			bccd.setCreate_time(now);
			this.getMapper(BaseCostCptDMapper.class).insert(bccd);
		}				
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 删除成本分配规则从表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseCostCptD(CommonPage<BaseCostCptD> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostCptD bccd = cp.getObject();
		if (bccd.getDst_item_code_f()==null || "".equals(bccd.getDst_item_code_f())) {
			throw new Exception("分配目标项目代码不能为空");
		}
		if (bccd.getDst_item_code()==null || "".equals(bccd.getDst_item_code())) {
			throw new Exception("分配依据项目代码不能为空");
		}	
		this.getMapper(BaseCostCptDMapper.class).delete(bccd.getDst_item_code_f(),bccd.getDst_item_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 成本分配规则从表：修改
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doUpdateBaseCostCptDFormula(CommonPage<BaseCostCptD> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm  = new ResultModel();
		BaseCostCptD bccd = cp.getObject();
		String opr = cp.getMemo1();
		if (bccd.getDst_item_code_f()==null || "".equals(bccd.getDst_item_code_f())) {
			throw new Exception("分配目标项目代码不能为空");
		}
		if (bccd.getDst_item_code()==null || "".equals(bccd.getDst_item_code())) {
			throw new Exception("分配依据项目代码不能为空");
		}
		BaseCostCptD BCCD = this.getMapper(BaseCostCptDMapper.class).findById(bccd.getDst_item_code_f(),bccd.getDst_item_code());
		if (BCCD==null) {
			throw new Exception("当前选择的成本分配规则明细不存在");
		}
		bccd.setUpdate_man(userName);
		bccd.setUpdate_time(new Date());
		this.getMapper(BaseCostCptDMapper.class).update(bccd);
		rm.setState("200");
		rm.setMsgInfo(opr+"成功");
		return rm;
	}
	
}
