/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：物料基础信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Mat.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.pb.SpeOptBaseService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Mat.mapper.PurMatMMapper;
import market.Pms.Mat.mapper.PurMatUpdatepirceLogsMapper;
import market.Pms.Mat.service.PurMatMService;
import market.Pms.Mat.vo.PurMatM;
import market.Pms.Mat.vo.PurMatUpdatepirceLogs;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="PurMatMService")
public class PurMatMServiceImpl extends BaseServiceImpl<PurMatM,Object> implements PurMatMService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		PageModel<PurMatM> pm=new PageModel<PurMatM>(oci.getPageIndex(),oci.getPageSize());
		PurMatM queryCls=oci.getObject()==null?(new PurMatM()):oci.getObject();
		try{	
			if (queryCls.getMat_class_code()==null || "".equals(queryCls.getMat_class_code())) {
				return rm;
			}
			List<PurMatM> list=this.getMapper(PurMatMMapper.class).doQuery(queryCls, 
					(oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize());
			pm.setTotal(this.getMapper(PurMatMMapper.class).doQueryCount(queryCls));
		    pm.setRows(list);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 新增
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		for(PurMatM pb:oci.getObjectList()){
			rm = ValidData(pb,"0");
			if("500".equals(rm.getState())){
				return rm;
			}
		}
		
		int i=1;
		String sCode=(oci.getObjectList().get(0).getMat_class_code()==null?"":oci.getObjectList().get(0).getMat_class_code());
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try{
			List<PurMatM> queryClsList=this.getMapper(PurMatMMapper.class).findWithCondition(null, " Item_code like '"+sCode+"______' ", " Item_code desc ", 0, 1);
			if(queryClsList!=null&&queryClsList.size()>0){
				i=Integer.parseInt(queryClsList.get(0).getItem_code().substring(sCode.length()))+1;
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		
		for(PurMatM pb:oci.getObjectList()){
			pb.setSort_code(i+"");
			pb.setItem_code(sCode+String.format("%06d", i++));
			pb.setItem_idx(PbMethod.ChineseSpell(pb.getItem_name()));
			pb.setCreate_name(userName);
			pb.setCreate_userid(userId);
			pb.setCreate_time(new Date());
			this.getMapper(PurMatMMapper.class).insert(pb);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改,其中有效，无效则传编号跟状态即可
	 * @param oci 
	 * memo1 1正常修改  2则为有效无效处理
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		rm = ValidData(oci.getObject(),"1");
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			if(!"1".equals(oci.getMemo1())){
				if("1".equals(oci.getObject().getValidflag())){//有效则作废数据制空
					this.getMapper(PurMatMMapper.class).doUpdate(oci.getObject());
				}else{
					oci.getObject().setDelete_name(userName);
					oci.getObject().setDelete_userid(userId);
					oci.getObject().setDelete_time(new Date());
					this.getMapper(PurMatMMapper.class).update(oci.getObject());
				}
			}else{
				oci.getObject().setItem_idx(PbMethod.ChineseSpell(oci.getObject().getItem_name()));
				oci.getObject().setUpdate_name(userName);
				oci.getObject().setUpdate_userid(userId);
				oci.getObject().setUpdate_time(new Date());
				this.getMapper(PurMatMMapper.class).update(oci.getObject());
			}
			
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 修改排序号
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSaveSortWeb(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		for(PurMatM pb:oci.getObjectList()){
			if(pb.getSort_code()!=null&&!"".equals(pb.getSort_code())){
				pb.setUpdate_name(userName);
				pb.setUpdate_userid(userId);
				pb.setUpdate_time(new Date());
				this.getMapper(PurMatMMapper.class).update(pb);
			}
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改价格
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSavePriceWeb(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		int i=1;
		try{
			List<PurMatUpdatepirceLogs> queryClsList=this.getMapper(PurMatUpdatepirceLogsMapper.class).findWithCondition(null, " change_id like 'C%' ", " change_id desc ", 0, 1);
			if(queryClsList!=null&&queryClsList.size()>0){
				i=Integer.parseInt(queryClsList.get(0).getChange_id().substring(7))+1;
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		
		for(PurMatM pb:oci.getObjectList()){
			pb.setUpdate_name(userName);
			pb.setUpdate_userid(userId);
			pb.setUpdate_time(new Date());
			this.getMapper(PurMatMMapper.class).update(pb);
			
			PurMatM pq=this.getMapper(PurMatMMapper.class).findById(pb.getItem_code());
			PurMatUpdatepirceLogs pmul=new PurMatUpdatepirceLogs();
			pmul.setItem_code(pq.getItem_code());
			pmul.setItem_name(pq.getItem_name());
			pmul.setCreate_name(userName);
			pmul.setCreate_userid(userId);
			pmul.setCreate_time(new Date());
			pmul.setOld_value(pq.getPlan_price()+"");
			pmul.setNew_value(pb.getPlan_price()+"");
			pmul.setChange_id("C"+(new SimpleDateFormat("yyyyMM").format(new Date()))+String.format("%06d", i++));
			this.getMapper(PurMatUpdatepirceLogsMapper.class).insert(pmul);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @param sType 0新增  1修改
	 * @return
	 */
	public ResultModel ValidData(PurMatM oci,String sType){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年12月2日
	 * @description 通过物料编号查询 物料信息
	 */
	public ResultModel doQuaryPurMatMById(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		if (oci.getMemo1()!=null && !"".equals(oci.getMemo1())) {
			PurMatM pmm = this.getMapper(PurMatMMapper.class).findById(oci.getMemo1());
			rm.setData(pmm);
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 上传附件保存
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doUpLoadMatFile(CommonPage<PurMatM> cp) throws Exception{
		ResultModel rm= new ResultModel();
		PurMatM pmm = cp.getObject();
		if(cp.getMemo1()==null){
			throw new Exception("请传入附件路径");
		}
		if(!cp.getMemo1().contains("group1/M00")){
			pmm.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(cp.getMemo1()));
		}		
		this.getMapper(PurMatMMapper.class).update(pmm);
		rm.setState("200");
		rm.setMsgInfo("附件上传成功");
		return rm;
	}
	
	public ResultModel doQueryPurMatMForYei(CommonPage<PurMatM> oci){
		ResultModel rm=new ResultModel();
		try{
			String condition = "validflag = '1' and mat_class_code like 'W02%'";
			List<PurMatM> list=this.getMapper(PurMatMMapper.class).findWithCondition(null, condition, "mat_class_code,sort_code", 0, 0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}	
		return rm;
	}
	
}
