/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-12
 * 描述            ：标准成本项目维护
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseCostStdMapper;
import market.Cm.service.BaseCostStdService;
import market.Cm.vo.BaseCostStd;
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="BaseCostStdService")
public class BaseCostStdServiceImpl extends BaseServiceImpl<BaseCostStd,Object> implements BaseCostStdService {
	
	/**
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCostStd(CommonPage<BaseCostStd> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostStd bcs = cp.getObject();
		try {			
			List<BaseCostStd> lists = this.getMapper(BaseCostStdMapper.class).doQueryBaseCostStd(bcs);
			rm.setData(lists);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}

	/**
	 * 标准成本维护的维护
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertOrUpdateBaseCostStd(CommonPage<BaseCostStd> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		List<BaseCostStd> bcsList = cp.getObjectList();
		if (bcsList==null || bcsList.size()<=0) {
			throw new Exception("请传入参数");
		}
		for (BaseCostStd bcs : bcsList) {
			BaseCostStd BCS = this.getMapper(BaseCostStdMapper.class).findById(bcs.getItem_code(),bcs.getOrg_code(),bcs.getStd_ver());
			if (BCS==null) {
				bcs.setCreate_man(userName);
				bcs.setCreate_time(now);
				this.getMapper(BaseCostStdMapper.class).insert(bcs);
			}else {
				bcs.setUpdate_man(userName);
				bcs.setUpdate_time(now);
				this.getMapper(BaseCostStdMapper.class).doUpdateBaseCostStd(bcs);
			}
		}
		rm.setState("200");
		rm.setMsgInfo("保存成功");
		return rm;
	}
	
	/**
	 * 标准成本维护的审核及反审
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeBaseCostStdAuditFlg(CommonPage<BaseCostStd> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		BaseCostStd bci = cp.getObject();
		/*if (bci.getItem_code()==null || "".equals(bci.getItem_code())) {
			throw new Exception("请传入成本项目代码");
		}*/
		if (bci.getOrg_code()==null || "".equals(bci.getOrg_code())) {
			throw new Exception("请传入成本组织代码");
		}
		if (bci.getStd_ver()==null || "".equals(bci.getStd_ver())) {
			throw new Exception("请传入版本时间");
		}
		
		BaseCostStd BCS = new BaseCostStd();
		//BCS.setItem_code(bci.getItem_code());
		BCS.setOrg_code(bci.getOrg_code());
		BCS.setStd_ver(bci.getStd_ver());
		BCS.setAudit_flg("1".equals(opr)?"1":"0");
		List<BaseCostStd> bcsList = this.getMapper(BaseCostStdMapper.class).find(BCS, 0, 0);
		if (bcsList!=null && bcsList.size()>0) {
			throw new Exception("版本时间【"+bci.getStd_ver()+"】成本组织【"+bci.getOrg_name()+"】中存在"+("1".equals(opr)?"已审核":"未审核")+"的数据");
		}
		bci.setAudit_man(userName);
		bci.setAudit_time(new Date());
		bci.setAudit_flg(opr);
		this.getMapper(BaseCostStdMapper.class).doChangeBaseCostStdAuditFlg(bci);
		rm.setState("200");
		rm.setMsgInfo(("0".equals(cp.getMemo1())?"反审":"审核") + "操作成功");
		return rm;
	}
	
	/**
	 * 标准成本维护的导入
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doImportBaseCostStd() throws Exception{
		synchronized (this) {		
			ResultModel rm = new ResultModel();
			//TODO
			rm.setState("200");
			rm.setMsgInfo("表格导入成功！");
			return rm; 
		} 	
	}
	
}
