/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2021-07-12
 * 描述            ：成本月份-钢种价格管理
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.CmMonthBasePriceMapper;
import market.Cm.service.CmMonthBasePriceService;
import market.Cm.vo.CmMonthBasePrice;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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="CmMonthBasePriceService")
public class CmMonthBasePriceServiceImpl extends BaseServiceImpl<CmMonthBasePrice,Object> implements CmMonthBasePriceService {
	
	/**
	 * 成本月份-钢种价格：查询
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmMonthBasePrice(CommonPage<CmMonthBasePrice> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmMonthBasePrice cmbp = cp.getObject();		 
		try {
			String swhere = "";			
			if (cmbp.getPrd_num()!=null && !"".equals(cmbp.getPrd_num())) {
				swhere+=" and prd_num like '%"+cmbp.getPrd_num()+"%'";//成本月份
			}			
			if (StringUtils.isNotEmpty(cmbp.getParent_item_code())) {
				swhere += " and parent_item_code = '"+cmbp.getParent_item_code()+"'";
			}
			if (StringUtils.isNotEmpty(cmbp.getParent_item_name())) {
				swhere += " and parent_item_name like '%"+cmbp.getParent_item_name()+"%'";
			}
			List<CmMonthBasePrice> list = this.getMapper(CmMonthBasePriceMapper.class).findWithCondition(null, swhere, "prd_num desc,parent_item_code", 0, 0);
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 成本月份-钢种价格：新增
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertCmMonthBasePrice(CommonPage<CmMonthBasePrice> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		CmMonthBasePrice cmbp = cp.getObject();
		if (StringUtils.isEmpty(cmbp.getPrd_num())) {
			throw new Exception("月份不能为空");
		}
		if (StringUtils.isEmpty(cmbp.getParent_item_code())) {
			throw new Exception("产品分类不能为空");
		}
		if (StringUtils.isEmpty(cmbp.getPrice())) {
			throw new Exception("价格不能为空");
		}
		//判断月份内的钢种价格是否重复
		CmMonthBasePrice CMBP = new CmMonthBasePrice();
		CMBP.setPrd_num(cmbp.getPrd_num());
		CMBP.setParent_item_code(cmbp.getParent_item_code());
		int count = this.getMapper(CmMonthBasePriceMapper.class).count(CMBP);
		if (count>0) {
			throw new Exception("月份【"+cmbp.getPrd_num()+"】内已经存在钢种【"+cmbp.getParent_item_name()+"】的价格数据，无法继续新增");
		}
		cmbp.setCreate_man(userName);
		cmbp.setCreate_time(new Date());
		this.getMapper(CmMonthBasePriceMapper.class).insert(cmbp);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 成本月份-钢种价格：修改
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doUpdateCmMonthBasePrice(CommonPage<CmMonthBasePrice> cp){
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		 ResultModel rm = new ResultModel();
		CmMonthBasePrice cmbp = cp.getObject();
		String sql = "update cm_month_base_price t set t.price = '"+cmbp.getPrice()+"',"
				+ " t.memo = '"+cmbp.getMemo()+"',"
				+ " t.update_man = '"+userName+"',"
				+ " t.update_time = to_date('"+now+"','yyyy-MM-dd hh24:mi:ss')"
				+ " where t.prd_num = '"+cmbp.getPrd_num()+"' "
				+ " and t.parent_item_code = '"+cmbp.getParent_item_code()+"'";
		this.getMapper(CmMonthBasePriceMapper.class).doUpdateCmMonthBasePrice(sql);
		 rm.setState("200");
		 rm.setMsgInfo("修改成功");
		 return rm;
	 }
	
	/**
	 * 成本月份-钢种价格：删除
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteCmMonthBasePrice(CommonPage<CmMonthBasePrice> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<CmMonthBasePrice> cmbpList = cp.getObjectList();
		if (cmbpList==null || cmbpList.size()<=0) {
			throw new Exception("请传入参数");
		}
		
		for (CmMonthBasePrice cai : cmbpList) {
			CmMonthBasePrice CMBP = this.getMapper(CmMonthBasePriceMapper.class).findById(cai.getPrd_num(),cai.getParent_item_code());
			if (CMBP == null) {
				throw new Exception("月份【"+cai.getPrd_num()+"】内不存在钢种类别【"+cai.getParent_item_name()+"】的价格维护数据");
			}
			this.getMapper(CmMonthBasePriceMapper.class).delete(cai.getPrd_num(),cai.getParent_item_code());
		}		
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;		
	}
	
	/**
	  * 复制价格
	  * @param cp [{"memo1":"2020-09","memo2":"2020-10"}]  memo1：需要复制的月份；  memo2：复制到哪个月份
	  * @return
	 * @throws Exception 
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doCopyCmMonthBasePrice(CommonPage<CmMonthBasePrice> cp) throws Exception{
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 Date now = new Date();
		 ResultModel rm = new ResultModel();
		 //1.1 判断复制到月份memo2是否存在数据
		 String condition = "prd_num = '"+cp.getMemo2()+"'";
		 int count = this.getMapper(CmMonthBasePriceMapper.class).countWithCondition(null, condition);
		 if (count > 0) {
			throw new Exception("月份【"+cp.getMemo2()+"】已经存在价格数据，无法复制");
		 }
		 //1.2 判断复制来源月份memo1是否存在数据
		 condition = "prd_num = '"+cp.getMemo1()+"'";
		 count = this.getMapper(CmMonthBasePriceMapper.class).countWithCondition(null, condition);
		 if (count <= 0) {
			throw new Exception("未找到月份【"+cp.getMemo1()+"】存在的价格数据，无法复制");
		 }
		 CmMonthBasePrice r = new CmMonthBasePrice();
		 r.setPrd_num(cp.getMemo1());
		 List<CmMonthBasePrice> cmbpList = this.getMapper(CmMonthBasePriceMapper.class).find(r,0,0);
		 if (CollectionUtils.isNotEmpty(cmbpList)) {
			for (CmMonthBasePrice cmbp : cmbpList) {
				CmMonthBasePrice rbp = new CmMonthBasePrice();
				rbp.setPrd_num(cp.getMemo2());
				rbp.setParent_item_code(cmbp.getParent_item_code());
				rbp.setParent_item_name(cmbp.getParent_item_name());
				rbp.setPrice(cmbp.getPrice());
				rbp.setMemo(cmbp.getMemo());
				rbp.setCreate_man(userName);
				rbp.setCreate_time(now);
				this.getMapper(CmMonthBasePriceMapper.class).insert(rbp);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("复制成功");
		 return rm;
	 }
	
}
