/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-05-30
 * 描述            ：成本组织基础表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseCostOrgMapper;
import market.Cm.mapper.BaseOrgCmpMapper;
import market.Cm.service.BaseCostOrgService;
import market.Cm.vo.BaseCostOrg;
import market.Cm.vo.BaseOrgCmp;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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="BaseCostOrgService")
public class BaseCostOrgServiceImpl extends BaseServiceImpl<BaseCostOrg,Object> implements BaseCostOrgService {

	/**
	 * 查询成本组织信息
	 * @param cp 
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryBaseCostOrg(CommonPage<BaseCostOrg> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		BaseCostOrg bco = cp.getObject();
		try {
			String swhere = "";
			if (bco.getOrg_name()!=null && !"".equals(bco.getOrg_name())) {
				swhere+=" and org_name like  '%"+bco.getOrg_name()+"%'";//组织名称
			}
			if (bco.getOrg_type_name()!=null && !"".equals(bco.getOrg_type_name())) {
				swhere+=" and org_type_name like  '%"+bco.getOrg_type_name()+"%'";//组织类型
			}
			if (bco.getOrg_type_code()!=null && !"".equals(bco.getOrg_type_code())) {
				swhere+=" and org_type_code = '"+bco.getOrg_type_code()+"'";//组织类型代码
			}
			if (bco.getOrg_char_name()!=null && !"".equals(bco.getOrg_char_name())) {
				swhere+=" and org_char_name like  '%"+bco.getOrg_char_name()+"%'";//组织特点
			}
			if (bco.getOrg_char_code()!=null && !"".equals(bco.getOrg_char_code())) {
				swhere+=" and org_char_code = '"+bco.getOrg_char_code()+"'";//组织特点代码
			}
			if (bco.getCost_detail_code()!=null && !"".equals(bco.getCost_detail_code())) {
				swhere+=" and cost_detail_code = '"+bco.getCost_detail_code()+"'";//核算粒度
			}
			if (bco.getIs_prod()!=null && !"".equals(bco.getIs_prod())) {//是否生产产线
				swhere+=" and is_prod = '"+bco.getIs_prod()+"'";
			}
			if ("1".equals(bco.getValid_flg())) {
				swhere+=" and valid_flg = '1'"; //是否有效
			}
			List<BaseCostOrg> lists = this.getMapper(BaseCostOrgMapper.class).findWithCondition(null, swhere, "index_id", 0, 0);
			rm.setData(lists);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败:"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增成本组织基础信息
	 * @param cp object[org_name组织名称,index_id显示顺序]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertBaseCostOrg(CommonPage<BaseCostOrg> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		BaseCostOrg bco = cp.getObject();
		if (bco.getOrg_name()==null || "".equals(bco.getOrg_name())) {
			throw new Exception("组织名称不能为空");
		}
		if (bco.getIndex_id()==null) {
			throw new Exception("显示顺序不能为空");
		}
		BaseCostOrg BCO = new BaseCostOrg();
		BCO.setOrg_name(bco.getOrg_name());
		int count = this.getMapper(BaseCostOrgMapper.class).count(BCO);
		if (count > 0) {
			throw new Exception("存在相同名称的成本组织信息");
		}
		
		//生成组织代码
		List<BaseCostOrg> list = this.getMapper(BaseCostOrgMapper.class).findWithCondition(null, null, "org_code desc", 0, 0);
		if (list==null || list.size()<=0) {
			bco.setOrg_code("C001");
		}else {
			String maxCode = list.get(0).getOrg_code();
			String seq = maxCode.substring(maxCode.length()-3);
			int iseq = Integer.parseInt(seq) + 1;
			bco.setOrg_code("C" + String.format("%03d", iseq));
		}
		bco.setCreate_man(userName);
		bco.setCreate_time(new Date());
		this.getMapper(BaseCostOrgMapper.class).insert(bco);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 修改成本组织基础信息
	 * @param cp object[org_code组织代码,org_name组织名称,index_id显示顺序]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateBaseCostOrg(CommonPage<BaseCostOrg> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		BaseCostOrg bco = cp.getObject();
		if (bco.getOrg_name()==null || "".equals(bco.getOrg_name())) {
			throw new Exception("组织名称不能为空");
		}
		if (bco.getIndex_id()==null) {
			throw new Exception("显示顺序不能为空");
		}
		
		BaseCostOrg BCO = this.getMapper(BaseCostOrgMapper.class).findById(bco.getOrg_code());
		if (BCO==null) {
			throw new Exception("当前选择行的成本组织不存在，请重新查询");
		}
		if ("0".equals(BCO.getValid_flg())) {
			throw new Exception("成本组织【"+BCO.getOrg_name()+"】已经禁用，无法修改");
		}
		
		//判断名称是否重复
		String condition = "org_code <> '"+bco.getOrg_code()+"' and org_name = '"+bco.getOrg_name()+"' ";
		List<BaseCostOrg> list = this.getMapper(BaseCostOrgMapper.class).findWithCondition(null, condition, null, 0, 0);
		if (list!=null && list.size()>0) {
			throw new Exception("存在相同的组织名称，无法修改");
		}
		bco.setUpdate_man(userName);
		bco.setUpdate_time(new Date());
		this.getMapper(BaseCostOrgMapper.class).update(bco);
		//同步修改组织对应关系下的组织名称
		this.getMapper(BaseOrgCmpMapper.class).updateBaseOrgCmpByOrgcode(bco.getOrg_code(),bco.getOrg_name());
		rm.setState("200");
		rm.setMsgInfo("修改成功");
		return rm;
	}
	
	/**
	 * 删除成本组织基础信息
	 * @param cp object[org_code组织代码]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseCostOrg(CommonPage<BaseCostOrg> cp) throws Exception{
		ResultModel rm = new ResultModel();
		BaseCostOrg bco = cp.getObject();
		if (bco.getOrg_code()==null || "".equals(bco.getOrg_code())) {
			throw new Exception("请传入组织代码");
		}
		//判断组织下的是否有组织对应关系
		BaseOrgCmp boc = new BaseOrgCmp();
		boc.setOrg_code(bco.getOrg_code());
		List<BaseOrgCmp> list = this.getMapper(BaseOrgCmpMapper.class).find(boc, 0, 0);
		if (list!=null && list.size()>0) {
			throw new Exception("当前成本组织下存在组织对应关系，请先删除组织对应关系");
		}
		this.getMapper(BaseCostOrgMapper.class).delete(bco.getOrg_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 启用/禁止 成本组织基础信息
	 * @param cp object[org_code组织代码] memo1[0:禁用    1:启用]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeValidFlg(CommonPage<BaseCostOrg> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		BaseCostOrg bco = cp.getObject();
		if (bco.getOrg_code()==null || "".equals(bco.getOrg_code())) {
			throw new Exception("请传入组织代码");
		}
		BaseCostOrg BCO = this.getMapper(BaseCostOrgMapper.class).findById(bco.getOrg_code());
		if (BCO==null) {
			throw new Exception("未找到传入的成本组织信息");
		}
		bco.setUpdate_man(userName);
		bco.setUpdate_time(new Date());
		if ("1".equals(opr)) {//启用
			if ("1".equals(BCO.getValid_flg())) {
				throw new Exception("成本组织"+BCO.getOrg_name()+"已经启用，无需继续启用");
			}
			bco.setValid_flg("1");
		}else if ("0".equals(opr)) {//禁用
			if ("0".equals(BCO.getValid_flg())) {
				throw new Exception("成本组织"+BCO.getOrg_name()+"已经禁用，无法继续禁用");
			}
			bco.setValid_flg("0");
		}
		this.getMapper(BaseCostOrgMapper.class).update(bco);
		rm.setState("200");
		rm.setMsgInfo(("0".equals(cp.getMemo1())?"禁用":"启用") + "操作成功");
		return rm;
	}
	
}
