/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-15
 * 描述            ：合法性检查历史表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseCptHistoryMapper;
import market.Cm.mapper.BaseCptIndexMapper;
import market.Cm.service.BaseCptHistoryService;
import market.Cm.vo.BaseCptHistory;
import market.Cm.vo.BaseCptIndex;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.vo.BaseDeliverystate;

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="BaseCptHistoryService")
public class BaseCptHistoryServiceImpl extends BaseServiceImpl<BaseCptHistory,Object> implements BaseCptHistoryService {
	
	/**
	 * 合法性历史查询
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCptHistory(CommonPage<BaseCptHistory> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCptHistory bch = cp.getObject();
		try {
			//1.查询最近的检查日期
			String cpt_date = this.getMapper(BaseCptIndexMapper.class).doQuaryMaxCptDate(bch);
			if (cpt_date!=null && !"".equals(cpt_date)) {				
				bch.setCpt_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(cpt_date));
			}
			List<BaseCptIndex> lists = this.getMapper(BaseCptIndexMapper.class).doQueryBaseCptHistory(bch);
			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 doInsertBaseCptHistory(CommonPage<BaseCptIndex> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		List<BaseCptIndex> bciList = cp.getObjectList();
		if (bciList==null || bciList.size()<=0) {
			throw new Exception("请传入参数");
		}
		if (cp.getMemo1()==null || "".equals(cp.getMemo1())) {
			throw new Exception("请传入成本期间");
		}
		
		//判断当天是否已经检查，如何已经检查，就删除当天的检查数据
		String condition = " and to_char(cpt_date, 'yyyy-MM-dd') = to_char(sysdate, 'yyyy-MM-dd')";
		int count = this.getMapper(BaseCptHistoryMapper.class).countWithCondition(null, condition);
		if (count > 0) {
			this.getMapper(BaseCptHistoryMapper.class).doDeleteTodaysBaseCptHistory();			
		}
		
		for (BaseCptIndex bci : bciList) {
			BaseCptHistory bch = new BaseCptHistory();
			bch.setCpt_item_code(bci.getCpt_item_code());//核算项目代码
			bch.setCpt_item_name(bci.getCpt_item_name());
			bch.setIs_auto_cpt(bci.getIs_auto_cpt());//是否核算检查(0检查、1核算)
			bch.setCpt_state(bci.getCpt_state());//核算状态
			bch.setMemo(bci.getMemo());
			bch.setCpt_date(now);
			bch.setPrd_num(cp.getMemo1());//成本期间
			bch.setValid_flg("1");
			bch.setCreate_man(userName);
			bch.setCreate_time(now);	
			this.getMapper(BaseCptHistoryMapper.class).insert(bch);					
		}
		rm.setState("200");
		rm.setMsgInfo("新增合法性检查历史表成功");
		return rm;
	}
	
	/**
	 * 交货状态下拉框
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryComBaseDeliveryState(CommonPage<BaseCptIndex> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		try {			
			List<BaseDeliverystate> list = this.getMapper(BaseCptIndexMapper.class).doQueryComBaseDeliveryState();
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
}
