/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-01-14
 * 描述            ：成本会计期间
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.BaseFiscalPeriodMapper;
import market.Cm.service.BaseFiscalPeriodService;
import market.Cm.vo.BaseFiscalPeriod;
import market.Cm.vo.BaseFiscalPeriodD;
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="BaseFiscalPeriodService")
public class BaseFiscalPeriodServiceImpl extends BaseServiceImpl<BaseFiscalPeriod,Object> implements BaseFiscalPeriodService {
	
	/**
	 * 动作处理_刷新（成本会计期间）按钮
	 * @param cp object[fsc_year会计年度 ,valid_flg是否审核]
	 * @return
	 */
	public ResultModel doQueryBaseFiscalPeriod(CommonPage<BaseFiscalPeriod> cp){
		ResultModel rm=new ResultModel();
		BaseFiscalPeriod bfp = cp.getObject();
		try{
			String swhere = "";
			if (bfp.getFsc_year()!=null && !"".equals(bfp.getFsc_year())) {
				swhere+=" and fsc_year like '%"+bfp.getFsc_year()+"%'";//会计年度
			}
			if (bfp.getValid_flg()!=null && !"".equals(bfp.getValid_flg())) {
				swhere+=" and valid_flg like '%"+bfp.getValid_flg()+"%'";//会计年度
			}
			List<BaseFiscalPeriod> lists = this.getMapper(BaseFiscalPeriodMapper.class).findWithCondition(null, swhere, "fsc_year desc", 0, 0);
			rm.setData(lists);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败:"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 新增成本会计期间
	 * @param cp  memo1期间个数,memo2起始日期,memo3终止日期,memo4是否自然年度,memo5备注,
	 * 			  ObjectList[fsc_year会计年度,prd_num成本期间,str_date起始时间,end_date终止时间]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertBaseFiscalPeriod(CommonPage<BaseFiscalPeriodD> cp) throws Exception{
		ResultModel rm=new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		if (cp.getMemo1()==null || "".equals(cp.getMemo1())) {
			throw new Exception("期间个数不能为空");
		}
		if (cp.getMemo2()==null || "".equals(cp.getMemo2())) {
			throw new Exception("起始日期不能为空");
		}
		if (cp.getMemo3()==null || "".equals(cp.getMemo3())) {
			throw new Exception("终止日期不能为空");
		}		
		try{
			List<BaseFiscalPeriodD> bfpdList = cp.getObjectList();
			Date now = new Date();
			if (bfpdList!=null && bfpdList.size()>0) {
				//验证会计年度是否存在
				String fsc_year = bfpdList.get(0).getFsc_year();
				BaseFiscalPeriod BFP = this.getMapper(BaseFiscalPeriodMapper.class).findById(fsc_year);
				if (BFP!=null) {
					throw new Exception("当前成本期间的会计年度已经存在");
				}				
				//新增成本会计期间(主表)
				BaseFiscalPeriod bfd = new BaseFiscalPeriod();
				bfd.setFsc_year(fsc_year);
				bfd.setPrd_cnt(Long.parseLong(cp.getMemo1()));//期间个数
				bfd.setStr_date(cp.getMemo2());		//开始日期
				bfd.setEnd_date(cp.getMemo3());		//结束日期
				bfd.setNatural_year(cp.getMemo4());	//是不是自然年度会计期间(1：是、0：否)
				bfd.setMemo(cp.getMemo5());			//备注				 
				bfd.setCreate_man(userName);
				bfd.setCreate_time(now);
				this.getMapper(BaseFiscalPeriodMapper.class).insert(bfd);
				//新增成本会计期间(从表)
				for (BaseFiscalPeriodD bfpd : bfpdList) {					
					bfpd.setCreate_man(userName);
					bfpd.setCreate_time(now);
					this.getMapper(BaseFiscalPeriodDMapper.class).insert(bfpd);
				}
			}
		}catch(Exception ex){
			throw new Exception("新增失败,请联系管理员");
		}
		rm.setMsgInfo("新增成功");
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改成本期间
	 * @param cp memo1期间个数,memo2起始日期,memo3终止日期,memo4是否自然年度,memo5备注,
	 * 			 ObjectList[fsc_year会计年度,prd_num成本期间,str_date起始时间,end_date终止时间]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateBaseFiscalPeriod(CommonPage<BaseFiscalPeriodD> cp) throws Exception{
		ResultModel rm=new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		if (cp.getMemo1()==null || "".equals(cp.getMemo1())) {
			throw new Exception("期间个数不能为空");
		}
		if (cp.getMemo2()==null || "".equals(cp.getMemo2())) {
			throw new Exception("起始日期不能为空");
		}
		if (cp.getMemo3()==null || "".equals(cp.getMemo3())) {
			throw new Exception("终止日期不能为空");
		}
		try{
			List<BaseFiscalPeriodD> bfpdList = cp.getObjectList();
			Date now = new Date();
			if (bfpdList!=null && bfpdList.size()>0) {
				//验证会计年度是否存在
				String fsc_year = bfpdList.get(0).getFsc_year();
				BaseFiscalPeriod BFP = this.getMapper(BaseFiscalPeriodMapper.class).findById(fsc_year);
				if (BFP==null) {
					throw new Exception("当前成本期间的会计年度不存在");
				}
				//修改成本会计期间(主表)
				BaseFiscalPeriod bfd = new BaseFiscalPeriod();
				bfd.setFsc_year(fsc_year);
				bfd.setPrd_cnt(Long.parseLong(cp.getMemo1()));//期间个数
				bfd.setStr_date(cp.getMemo2());		//开始日期
				bfd.setEnd_date(cp.getMemo3());		//结束日期
				bfd.setNatural_year(cp.getMemo4());	//是不是自然年度会计期间(1：是、0：否)
				bfd.setMemo(cp.getMemo5());			//备注
				bfd.setUpdate_man(userName);
				bfd.setUpdate_time(now);
				bfd.setValid_flg("1");
				this.getMapper(BaseFiscalPeriodMapper.class).update(bfd);
				//删掉以前的成本期间记录信息，同时新增新的成本期间信息
				this.getMapper(BaseFiscalPeriodDMapper.class).deleteFiscalPeriodDByFscYear(bfd.getFsc_year());
				for (BaseFiscalPeriodD bfpd : bfpdList) {			
					bfpd.setCreate_man(userName);
					bfpd.setCreate_time(now);
					this.getMapper(BaseFiscalPeriodDMapper.class).insert(bfpd);
				}
			}
		}catch(Exception ex){
			throw new Exception("修改失败,请联系管理员");
		}
		rm.setMsgInfo("修改成功");
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除成本期间
	 * @param cp object[fsc_year会计年度]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteBaseFiscalPeriod(CommonPage<BaseFiscalPeriod> cp) throws Exception{
		ResultModel rm=new ResultModel();
		BaseFiscalPeriod bfp = cp.getObject();
		try {
			BaseFiscalPeriod BFP = this.getMapper(BaseFiscalPeriodMapper.class).findById(bfp.getFsc_year());
			if (BFP!=null) {
				//删除成本期间主表
				this.getMapper(BaseFiscalPeriodMapper.class).delete(BFP.getFsc_year());
				//删除成本期间子表
				this.getMapper(BaseFiscalPeriodDMapper.class).deleteFiscalPeriodDByFscYear(BFP.getFsc_year());				
			}
		} catch (Exception ex) {
			throw new Exception("删除失败:"+ex.getMessage());
		}
		rm.setMsgInfo("删除成功");
		rm.setState("200");
	    return rm;
	}
	
	/**
	 * 设置启用期间
	 * @param cp  object[prd_num成本期间]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doSetInitPeriod(CommonPage<BaseFiscalPeriodD> cp) throws Exception{
		ResultModel rm=new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			BaseFiscalPeriodD bfpd = cp.getObject();
			//验证会计期间(子表)
			BaseFiscalPeriodD BFPD = this.getMapper(BaseFiscalPeriodDMapper.class).findById(bfpd.getPrd_num());
			if (BFPD==null) {
				throw new Exception("当前成本会计明细期间信息不存在");
			}
			//验证会计期间(主表)
			BaseFiscalPeriod BFP = this.getMapper(BaseFiscalPeriodMapper.class).findById(BFPD.getFsc_year());
			if (BFP==null) {
				throw new Exception("当前成本会计期间主信息不存在");
			}
			//启用期间只能有一个：将原先的启用期间去除
			this.getMapper(BaseFiscalPeriodDMapper.class).updateBaseFiscalPeriodDInit_prd("0", "1");
			bfpd.setInit_prd("1");
			bfpd.setUpdate_man(userName);
			bfpd.setUpdate_time(new Date());
			this.getMapper(BaseFiscalPeriodDMapper.class).update(bfpd);			
		}catch(Exception ex){
			throw new Exception("设定启用期间失败:"+ex.getMessage());
		}
		rm.setState("200");
		rm.setMsgInfo("设定启用期间成功");
		return rm;
	}
	
}
