/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-06-13
 * 描述            ：分配依据维护表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseDstBasisMapper;
import market.Cm.service.BaseDstBasisService;
import market.Cm.vo.BaseDstBasis;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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

@RemoteService(path="BaseDstBasisService")
public class BaseDstBasisServiceImpl extends BaseServiceImpl<BaseDstBasis,Object> implements BaseDstBasisService {
	
	/**
	 * 查询分配依据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseDstBasis(CommonPage<BaseDstBasis> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseDstBasis bdb = cp.getObject();
		try {
			String swhere = "";
			if (bdb.getDst_basis_name()!=null && !"".equals(bdb.getDst_basis_name())) {
				swhere+=" and dst_basis_name like '%"+bdb.getDst_basis_name()+"%'";//分配依据名称
			}
			if (bdb.getDst_basis_dsc()!=null && !"".equals(bdb.getDst_basis_dsc())) {
				swhere+=" and dst_basis_dsc like '%"+bdb.getDst_basis_dsc()+"%'";//分配依据说明
			}
			if ("1".equals(bdb.getValid_flg())) {
				swhere+=" and valid_flg = '1'"; //是否有效
			}
			List<BaseDstBasis> lists = this.getMapper(BaseDstBasisMapper.class).findWithCondition(null, swhere, "create_time desc", 0, 0);
			rm.setData(lists);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 新增分配依据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertBaseDstBasis(CommonPage<BaseDstBasis> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		BaseDstBasis bdb = cp.getObject();
		if (bdb.getDst_basis_name()==null || "".equals(bdb.getDst_basis_name())) {
			throw new Exception("分配依据名称不能为空");
		}		
		BaseDstBasis BDB = new BaseDstBasis();
		BDB.setDst_basis_name(bdb.getDst_basis_name());
		int count = this.getMapper(BaseDstBasisMapper.class).count(BDB);
		if (count > 0) {
			throw new Exception("分配依据名称【"+bdb.getDst_basis_name()+"】已经存在，无法新增");
		}
		
		//生成组织代码
		List<BaseDstBasis> list = this.getMapper(BaseDstBasisMapper.class).findWithCondition(null, null, "dst_basis_code desc", 0, 0);
		if (list==null || list.size()<=0) {
			bdb.setDst_basis_code("CB001");
		}else {
			String maxCode = list.get(0).getDst_basis_code();
			String seq = maxCode.substring(maxCode.length()-3);
			int iseq = Integer.parseInt(seq) + 1;
			bdb.setDst_basis_code("CB" + String.format("%03d", iseq));
		}
		bdb.setCreate_man(userName);
		bdb.setCreate_time(new Date());
		this.getMapper(BaseDstBasisMapper.class).insert(bdb);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 修改
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doUpdateBaseDstBasis(CommonPage<BaseDstBasis> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		BaseDstBasis bdb = cp.getObject();
		if (bdb.getDst_basis_code()==null || "".equals(bdb.getDst_basis_code())) {
			throw new Exception("分配依据代码不能为空");
		}
		if (bdb.getDst_basis_name()==null || "".equals(bdb.getDst_basis_name())) {
			throw new Exception("分配依据名称不能为空");
		}
		
		BaseDstBasis BDB = this.getMapper(BaseDstBasisMapper.class).findById(bdb.getDst_basis_code());
		if (BDB==null) {
			throw new Exception("当前选择的分配依据不存在，请重新查询");
		}
		if ("0".equals(BDB.getValid_flg())) {
			throw new Exception("分配依据【"+bdb.getDst_basis_name()+"】已经禁用，无法修改");
		}
		
		//判断名称是否重复
		String condition = "dst_basis_code <> '"+bdb.getDst_basis_code()+"' and dst_basis_name = '"+bdb.getDst_basis_name()+"' ";
		List<BaseDstBasis> list = this.getMapper(BaseDstBasisMapper.class).findWithCondition(null, condition, null, 0, 0);
		if (list!=null && list.size()>0) {
			throw new Exception("分配依据名称【"+bdb.getDst_basis_name()+"】已经存在，无法修改");
		}
		bdb.setUpdate_man(userName);
		bdb.setUpdate_time(new Date());
		this.getMapper(BaseDstBasisMapper.class).update(bdb);		
		rm.setState("200");
		rm.setMsgInfo("修改成功");
		return rm;
	}
	
	/**
	 * 删除分配依据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseDstBasis(CommonPage<BaseDstBasis> cp) throws Exception{
		ResultModel rm = new ResultModel();
		BaseDstBasis bdb = cp.getObject();
		if (bdb.getDst_basis_code()==null || "".equals(bdb.getDst_basis_code())) {
			throw new Exception("请传入分配依据代码");
		}		 
		this.getMapper(BaseDstBasisMapper.class).delete(bdb.getDst_basis_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 启用、禁用分配依据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeBaseDstBasisValidFlg(CommonPage<BaseDstBasis> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		String reslut = null;
		BaseDstBasis bdb = cp.getObject();
		if (bdb.getDst_basis_code()==null || "".equals(bdb.getDst_basis_code())) {
			throw new Exception("请传入分配依据代码");
		}
		bdb.setUpdate_man(userName);
		bdb.setUpdate_time(new Date());
		if ("1".equals(opr)) {//启用
			bdb.setValid_flg("1");
			reslut = "启用";
		}else if ("0".equals(opr)) { //禁用
			bdb.setValid_flg("0");
			reslut = "禁用";
		}
		this.getMapper(BaseDstBasisMapper.class).update(bdb);
		rm.setState("200");
		rm.setMsgInfo(reslut + "操作成功");
		return rm;
	}
	
	
}
