/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-14
 * 描述            ：合同抬头表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Order.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Order.mapper.PurOrderTitleMapper;
import market.Pms.Order.service.PurOrderTitleService;
import market.Pms.Order.vo.PurOrderTitle;

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="PurOrderTitleService")
public class PurOrderTitleServiceImpl extends BaseServiceImpl<PurOrderTitle,Object> implements PurOrderTitleService {
	/**
	 * 合同抬头查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderTitle> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderTitle> pm=new PageModel<PurOrderTitle>(oci.getPageIndex(),oci.getPageSize());
		PurOrderTitle queryCls=oci.getObject()==null?(new PurOrderTitle()):oci.getObject();
		
		try{
			pm.setTotal(this.getMapper(PurOrderTitleMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurOrderTitleMapper.class).doQuery(queryCls, (oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize()));
			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<PurOrderTitle> oci) {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需新增的数据！");
			return rm;
		}
		try {
			for(PurOrderTitle pot:oci.getObjectList()){
				PurOrderTitle title = new PurOrderTitle();
				title.setTitle_name(pot.getTitle_name());
				List<PurOrderTitle> titleList = this.getMapper(PurOrderTitleMapper.class).find(title, 0, 0);
				if(titleList.size()>0){
					rm.setState("500");
					rm.setMsgInfo("新增失败！存在相同的买受人"+pot.getTitle_name());
					return rm;
				}
				pot.setTitle_code(this.getMapper(PurOrderTitleMapper.class).getNo("TT"+new SimpleDateFormat("yyyyMMdd").format(new Date())));
				pot.setValidflag("1"); // 有效数据
				pot.setCreate_userid(userId);
				pot.setCreate_name(userName);
				pot.setCreate_time(new Date());
				this.getMapper(PurOrderTitleMapper.class).insert(pot);
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 合同抬头操作(memo1：  1编辑  2启用 3停用)
	 * @param pot
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurOrderTitle> oci) {
		PurOrderTitle pot = oci.getObject(); // 抬头对象
		String memo1 = oci.getMemo1(); // 操作类型
		ResultModel rm = new ResultModel();
		try {
			if(pot!=null && memo1!=null && !memo1.isEmpty()){
				if("1".equals(memo1)){ // 修改
					rm = doUpdate(pot);
					return rm;
				}else if("2".equals(memo1)){ // 启用
					rm = doEnable(pot);
					return rm;
				}else if("3".equals(memo1)){ // 停用
					rm = doDisable(pot);
					return rm;
				}else{
					rm.setState("500");
					rm.setMsgInfo("无效的操作类型！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("操作失败！当前数据无效！");
				return rm;
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败："+ex.getMessage());
			return rm;
		}
	}
	
	// 编辑
	public ResultModel doUpdate(PurOrderTitle pot) throws Exception {
		ResultModel rm = new ResultModel();
		if(pot.getTitle_code()!=null && !pot.getTitle_code().isEmpty()){ // 判断主键抬头代码不为空
			PurOrderTitle title = this.getMapper(PurOrderTitleMapper.class).findById(pot.getTitle_code());
			if(title!=null){ // 判断数据不为空
				if("1".equals(title.getValidflag())){ // 判断数据有效
					if(pot.getTitle_name().equals(title.getTitle_name())){ // 判断不能编辑已存在的买受人
						// 更新数据
						doUpdateTitle(pot);
					}else{
						PurOrderTitle potTitle = new PurOrderTitle();
						potTitle.setTitle_name(pot.getTitle_name());
						List<PurOrderTitle> potTitleList = this.getMapper(PurOrderTitleMapper.class).find(potTitle, 0, 0);
						if(potTitleList.size()>0){
							rm.setState("500");
							rm.setMsgInfo("编辑失败！已存在相同的买受人"+pot.getTitle_name());
							return rm;
						}
						// 更新数据
						doUpdateTitle(pot);
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("编辑失败！该数据为无效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("编辑失败！该数据不存在！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("编辑失败！该数据为无效数据！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 启用
	public ResultModel doEnable(PurOrderTitle pot) throws Exception {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(pot.getTitle_code()!=null && !pot.getTitle_code().isEmpty()){ // 主键不为空
			PurOrderTitle title = this.getMapper(PurOrderTitleMapper.class).findById(pot.getTitle_code());
			if(title!=null){ // 数据库数据不为空
				if("0".equals(title.getValidflag())){ // 判断是否为有效数据
					title.setValidflag("1");
					title.setUpdate_userid(userId);
					title.setUpdate_name(userName);
					title.setUpdate_time(new Date());
					this.getMapper(PurOrderTitleMapper.class).update(title);
				}else{
					rm.setState("500");
					rm.setMsgInfo("启用失败！当前数据是有效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("启用失败！当前数据不存在！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("启用失败！该数据无效！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 合同抬头废除
	 * @param pot
	 * @return
	 */
	public ResultModel doDisable(PurOrderTitle pot) throws Exception {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(pot.getTitle_code()!=null && !pot.getTitle_code().isEmpty()){ // 主键不为空
			PurOrderTitle title = this.getMapper(PurOrderTitleMapper.class).findById(pot.getTitle_code());
			if(title!=null){ // 数据库数据不为空
				if("1".equals(title.getValidflag())){ // 判断是否为有效数据
					title.setValidflag("0");
					title.setDelete_userid(userId);
					title.setDelete_name(userName);
					title.setDelete_time(new Date());
					this.getMapper(PurOrderTitleMapper.class).update(title);
				}else{
					rm.setState("500");
					rm.setMsgInfo("废除失败！当前数据已经是无效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("废除失败！当前数据无效！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("废除失败！当前数据无效！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	
	// 逻辑方法
	public void doUpdateTitle(PurOrderTitle pot){
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		pot.setUpdate_userid(userId);
		pot.setUpdate_name(userName);
		pot.setUpdate_time(new Date());
		this.getMapper(PurOrderTitleMapper.class).update(pot);
	}
	
	/**
	 * 根据主键编码查询
	 * @param pot
	 * @return
	 */
	public PurOrderTitle doQueryById(PurOrderTitle pot){
		PurOrderTitle title = new PurOrderTitle();
		try {
			if(pot.getTitle_code()!=null && !pot.getTitle_code().isEmpty()){
				title = this.getMapper(PurOrderTitleMapper.class).findById(pot.getTitle_code());
			}
		} catch (Exception e) {
			return new PurOrderTitle();
		}
		return title;
	}
}
