/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			合同指定产线信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/
package market.Slm.ContractManagement.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.SlmBaseAddAskMapper;
import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Com.vo.SlmBaseAddAsk;
import market.Qms.common.DataHelper;
import market.Slm.ContractManagement.mapper.SlmContractLineInfoMapper;
import market.Slm.ContractManagement.mapper.SlmCustomerAddAskMapper;
import market.Slm.ContractManagement.service.SlmCustomerAddAskService;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;
import market.Slm.ContractManagement.vo.SlmCustomerAddAsk;
import market.Slm.OrderManagement.impl.SlmOrderInfoServiceImpl;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

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="SlmCustomerAddAskService")
public class SlmCustomerAddAskServiceImpl extends BaseServiceImpl<SlmCustomerAddAsk,Object> implements SlmCustomerAddAskService {

	/**
	 * 客户附加要求-左侧列表
	 * memo1合同行号
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerAddAsk> sum){
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		List<SlmCustomerAddAsk> list=new ArrayList<SlmCustomerAddAsk>();
		String line_no=sum.getMemo1();
		try{
			//质量模块+附加要求表
			SlmContractLineInfo line=this.getBean(SlmContractLineInfoServiceImpl.class).doQueryById(line_no);
			SlmCustomerAddAsk addask=new SlmCustomerAddAsk();
			addask.setAdd_ask_no(line.getStd_code());
			List<SlmCustomerAddAsk> askList=this.getMapper(SlmCustomerAddAskMapper.class).doQuery(ask);
			list.add(addask);
			list.addAll(askList);
			rm.setState("200");
			rm.setData(list);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 客户附加要求-右侧列表(点击左侧展示右侧)
	 * memo1:如果是第一条，传给此值
	 * */
	public ResultModel doQueryListWeb(CommonPage<SlmCustomerAddAsk> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		List<SlmCustomerAddAsk> list=new ArrayList<SlmCustomerAddAsk>();
		try{
			if(sum.getMemo1()!=null&&!"".equals(sum.getMemo1())){
				list=this.getMapper(SlmCustomerAddAskMapper.class).doQueryStd(sum.getMemo1());
			}else{
				list=this.getMapper(SlmCustomerAddAskMapper.class).find(ask,0,0);
			}
			rm.setState("200");
			rm.setData(list);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 客户附加要求-模板新增
	 * [{"customer_no":"","customer_name":"","list":[{},{}]}]
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerAddAsk> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String maxNo="";
		List<SlmCustomerAddAsk> maxAsk=this.getMapper(SlmCustomerAddAskMapper.class).findWithCondition(ask, null, " add_ask_no desc", 0, 0);
		if(maxAsk.size()>0){
			maxNo=maxAsk.get(0).getAdd_ask_no();
			maxNo=maxNo.substring(maxNo.length()-4);
			int no=Integer.parseInt(maxNo)+1;
			maxNo=ask.getCustomer_no()+String.format("%04d",no);	//新附加要求编码
		}else{
			maxNo=ask.getCustomer_no()+String.format("%04d", 1);
		}
		List<SlmCustomerAddAsk> list=ask.getList();
		if(list!=null){
			for(SlmCustomerAddAsk asks:list){
				asks.setAdd_ask_no(maxNo);
				asks.setCustomer_no(ask.getCustomer_no());
				if(ask.getCustomer_nm()!=null)
					asks.setCustomer_nm(ask.getCustomer_nm());
				if(asks.getAsk_sub_itme_val()==null||"".equals(asks.getAsk_sub_itme_val()))
					asks.setAsk_sub_itme_val(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
				if(asks.getAdd_ask_status()==null||"".equals(asks.getAdd_ask_status()))
					asks.setAdd_ask_status("1");
				asks.setCreate_man_no(userId);
				asks.setCreate_man_name(userName);
				asks.setCreate_time(new Date());
				asks.setValid_flag("1");
				this.getMapper(SlmCustomerAddAskMapper.class).doAdd(asks);
			}
		}
		rm.setState("200");
		return rm;
	}

	/**
	 * 客户附加要求-停用
	 * add_ask_no
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDisableWeb(CommonPage<SlmCustomerAddAsk> sum) {
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmCustomerAddAsk ask=sum.getObject();
		ask.setDelete_man_no(userId);
		ask.setDelete_man_name(userName);
		ask.setDelete_time(new Date());
		this.getMapper(SlmCustomerAddAskMapper.class).doDisable(ask);
		rm.setState("200");
		return rm;
	}

	/**
	 * 客户附加要求-选择
	 * 合同头号，合同行号
	 * memo2 :0合同行 1订单
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSelectWeb(CommonPage<SlmCustomerAddAsk> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		try{
			String desc="";
			String no="";
			int seq=1;
			List<SlmCustomerAddAsk> list=this.getMapper(SlmCustomerAddAskMapper.class).find(ask, 0, 0);
			for(SlmCustomerAddAsk asks:list){
				no=asks.getAdd_ask_no();
				desc+=seq+".要求类型："+doTransAddAskType(asks.getAdd_ask_type())+"，要求项："+asks.getAsk_itme_desc()+"，要求分项："+asks.getAsk_sub_itme_desc()+"，标准说明："+asks.getAsk_sub_itme_val_desc()+"。\r\n ";
				seq++;
			}
			if("1".equals(sum.getMemo2())){		//订单中的附加要求
				SlmOrderInfo order=this.getBean(SlmOrderInfoServiceImpl.class).doQueryOne(sum.getMemo1());
				if(order!=null){
					order.setAdd_ask_no(no);
					order.setAdd_ask_desc(desc);
					this.getBean(SlmOrderInfoServiceImpl.class).doUpdate(order);
				}
			}else{		//合同行中的附加要求
				SlmContractLineInfo line=this.getBean(SlmContractLineInfoServiceImpl.class).doQueryById(sum.getMemo1());
				if(line!=null){
					line.setAdd_ask_no(no);
					line.setAdd_ask_desc(desc);
					this.getMapper(SlmContractLineInfoMapper.class).update(line);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 客户附加要求-详情
	 * add_ask_no,ask_itme,ask_sub_itme三个字段 联合主键
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerAddAsk> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		try{
			List<SlmCustomerAddAsk> list=this.getMapper(SlmCustomerAddAskMapper.class).find(ask,0,0);
			if(list.size()>0)
				ask=list.get(0);
			rm.setState("200");
			rm.setData(ask);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * add_ask_type转换【0:标准项，1:修改项，2:新增项
	 * */
	public String doTransAddAskType(String type_no){
		String type="标准项";
		int no=Integer.parseInt(type_no);
		switch(no){
		case 1:
			type="修改项";
		case 2:
			type="新增项";
			default:break;
		}
		return type;
	}
	/**
	 * 客户附加要求-左侧列表(质量用)------jcb
	 * memo1合同行号
	 * */
	public ResultModel doQueryWeb1(CommonPage<SlmCustomerAddAsk> sum){
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		List<SlmCustomerAddAsk> list=new ArrayList<SlmCustomerAddAsk>();
		String line_no=sum.getMemo1();
		try{
			//质量模块+附加要求表
			SlmContractLineInfo line=this.getBean(SlmContractLineInfoServiceImpl.class).doQueryById(line_no);
			SlmCustomerAddAsk addask=new SlmCustomerAddAsk();
			addask.setAdd_ask_no(line.getStd_name());
			addask.setStd_code(line.getStd_code());
			List<String> liststr=new ArrayList<>();
			list.add(addask);
			List<SlmCustomerAddAsk> askList=this.getMapper(SlmCustomerAddAskMapper.class).doQuery(ask);
			for (SlmCustomerAddAsk al : askList) {
				if (!liststr.contains(al.getAdd_ask_no())) {
					list.add(al);
					liststr.add(al.getAdd_ask_no());
				}
			}
			rm.setState("200");
			rm.setData(list);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 客户附加要求-右侧列表(点击左侧展示右侧)质量模块用
	 * memo1:如果是第一条，传给此值
	 * */
	public ResultModel doQueryListWeb1(CommonPage<SlmCustomerAddAsk> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAddAsk ask=sum.getObject();
		List<SlmCustomerAddAsk> list=new ArrayList<SlmCustomerAddAsk>();
		try{
			if(!DataHelper.IsNullOrSpace(sum.getMemo1())){
				list=this.getMapper(SlmCustomerAddAskMapper.class).doQueryStd(sum.getMemo1());
				for (SlmCustomerAddAsk s : list) {
					s.setAdd_ask_type("0");
					List<SlmBaseAddAsk> listslb = this.getMapper(SlmBaseAddAskMapper.class).findWithCondition(null, "ask_p_item_no='"+s.getAsk_itme()+"'", null, 0, 0);
					for (SlmBaseAddAsk slb : listslb) {
						if (s.getAsk_sub_itme().equals(slb.getAsk_item_no())) {
							s.setIs_allowed(slb.getIs_allow_edit());
						}
					}
				}
			}else{
				list=this.getMapper(SlmCustomerAddAskMapper.class).find(ask,0,0);
				for (SlmCustomerAddAsk s : list) {
					List<SlmBaseAddAsk> listslb = this.getMapper(SlmBaseAddAskMapper.class).findWithCondition(null, "ask_p_item_no='"+s.getAsk_itme()+"'", null, 0, 0);
					for (SlmBaseAddAsk slb : listslb) {
						if (s.getAsk_sub_itme().equals(slb.getAsk_item_no())) {
							s.setIs_allowed(slb.getIs_allow_edit());
						}
					}
				}
			}
			rm.setState("200");
			rm.setData(list);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
}
