/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-11
 * 描述            ：销售年度计划信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.PlanManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.PbModelDb;
import market.Com.vo.ResultModel;
import market.Slm.PlanManagement.mapper.SlmSalePlanConfigMapper;
import market.Slm.PlanManagement.service.SlmSalePlanConfigService;
import market.Slm.PlanManagement.vo.SlmSalePlanConfig;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="SlmSalePlanConfigService")
public class SlmSalePlanConfigServiceImpl extends BaseServiceImpl<SlmSalePlanConfig,Object> implements SlmSalePlanConfigService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmSalePlanConfig> sum){
		ResultModel rm=new ResultModel();
		SlmSalePlanConfig area=sum.getObject();
		try{
			List<SlmSalePlanConfig> list=this.getMapper(SlmSalePlanConfigMapper.class).findWithCondition(area, null, " plan_class_no,plan_variety_no ", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(CommonPage<PbModelDb> sum){
		ResultModel rm=new ResultModel();
		List<PbModelDb> config_list=sum.getObjectList();
		SlmSalePlanConfig config=new SlmSalePlanConfig();
		try{
			String class_no=sum.getMemo1();
			String class_name =sum.getMemo2();
			String v_no=sum.getMemo3();
			String v_name=sum.getMemo4();
			String year=new SimpleDateFormat("yyyy").format(new Date());
			for(PbModelDb pb:config_list){
				List<SlmSalePlanConfig> spc=this.getMapper(SlmSalePlanConfigMapper.class).findWithCondition(null, " plan_config_no like concat('"+year+"','%')", "Plan_config_no desc", 0, 1);
				int count=0;
				if(spc!=null && spc.size()>0){
					count=Integer.parseInt(spc.get(0).getPlan_config_no().substring(4));
				}
				String id=year+String.format("%04d", count+1);
				config.setSteel_code(pb.getId());
				config.setSteel_name(pb.getText());
				config.setPlan_config_no(id);
				config.setPlan_class_no(class_no);
				config.setPlan_class_name(class_name);
				config.setPlan_variety_no(v_no);
				config.setPlan_variety_name(v_name); 
				this.getMapper(SlmSalePlanConfigMapper.class).insert(config);
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}


	/**
	 * 删除 物理删除
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmSalePlanConfig> sum){
		ResultModel rm=new ResultModel();
		List<SlmSalePlanConfig> config_list=sum.getObjectList();
		try{
			for(SlmSalePlanConfig config:config_list){
				SlmSalePlanConfig check=this.getMapper(SlmSalePlanConfigMapper.class).findById(config.getPlan_config_no());
				if(check!=null){
					this.getMapper(SlmSalePlanConfigMapper.class).delete(check.getPlan_config_no());
				}else{
//					rm.setMsgInfo("该数据已删除");
//					rm.setState("500");
//					return rm;
				}
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

}