/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-08-13
 * 描述            ：企业纪事
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.CompanyNew.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.pb.SpeOptBaseService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.CompanyNew.mapper.SlmCompanyNewAnnexMapper;
import market.Slm.CompanyNew.mapper.SlmCompanyNewMapper;
import market.Slm.CompanyNew.service.SlmCompanyNewService;
import market.Slm.CompanyNew.vo.SlmCompanyNew;
import market.Slm.CompanyNew.vo.SlmCompanyNewAnnex;

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="SlmCompanyNewService")
public class SlmCompanyNewServiceImpl extends BaseServiceImpl<SlmCompanyNew,Object> implements SlmCompanyNewService {
	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew area=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		String startTime=sum.getStartTime();		
		String endTime=sum.getEndTime();
		
		PageModel<SlmCompanyNew> pm=new PageModel<SlmCompanyNew>(pageIndex,pageSize);
		try{
			String sWhere="1=1";
			if(area!=null&&area.getCompany_new_name()!=null&&!"".equals(area.getCompany_new_name())){
				sWhere+="  and company_new_name like '%"+area.getCompany_new_name()+"%'";
				area.setCompany_new_name(null);
			}
			if(startTime!=null&&!"".equals(startTime)){
				startTime+=" 00:00:00";
				sWhere+=" and to_char(create_time, 'yyyy-mm-dd hh24:mi:ss') >='"+startTime+"'";
			}
			if(endTime!=null&&!"".equals(endTime)){
				endTime+=" 23:59:59";
				sWhere+=" and to_char(create_time, 'yyyy-mm-dd hh24:mi:ss') <= '"+endTime+"'";
			}
			
			List<SlmCompanyNew> list=this.getMapper(SlmCompanyNewMapper.class).findWithCondition(area, sWhere, " create_time desc", (pageIndex-1)*pageSize, pageSize);
			pm.setRows(list);
			pm.setTotal(this.getMapper(SlmCompanyNewMapper.class).countWithCondition(area,sWhere));
			
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * @throws Exception 
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCompanyNew> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmCompanyNew area=sum.getObject();
		try {
		String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		area.setCompany_new_no(id);
		area.setCreate_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
		area.setCreate_man_name((String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
		area.setCreate_time(new Date());
		area.setValid_flag("1");
		area.setApproval_flag("1");
		
		this.getMapper(SlmCompanyNewMapper.class).insert(area);
		int i=1;
		for(SlmCompanyNewAnnex annex: area.getAnnex()) {
			annex.setAnnex_no(id+String.format("%03d", i++));
			annex.setCompany_new_no(id);
			
			if(annex.getAnnex_path()==null){
				throw new Exception("请传入附件路径");
			}
			if(!annex.getAnnex_path().contains("group1/M00")){
				annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
			}
			
			annex.setPrvd_man_no(area.getCreate_man_no());
			annex.setPrvd_man_name(area.getCreate_man_name());
			annex.setPrvd_time(area.getCreate_time());
			this.getMapper(SlmCompanyNewAnnexMapper.class).insert(annex);
		}
		rm.setState("200");

		} catch (Exception e) {
			//--19-10-15添加异常回滚
			throw new Exception("新增失败！"+e.getMessage());
		}
		
		return rm;
	}

	/**
	 * 修改
	 * @throws Exception 
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmCompanyNew> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmCompanyNew area=sum.getObject();
		try {
		SlmCompanyNew check=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
		if(check!=null){
			if("1".equals(check.getValid_flag())&&"1".equals(check.getApproval_flag())){
				area.setUpdate_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
				area.setUpdate_man_name((String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
				area.setUpdate_time(new Date());
				this.getMapper(SlmCompanyNewMapper.class).update(area);
				
				if(area.getAnnex()!=null&&area.getAnnex().size()>0) {
					this.getMapper(SlmCompanyNewAnnexMapper.class).doDelete(area.getCompany_new_no());
					int i=1;
					for(SlmCompanyNewAnnex annex: area.getAnnex()) {
						annex.setAnnex_no(area.getCompany_new_no()+String.format("%03d", i++));
						annex.setCompany_new_no(area.getCompany_new_no());
						annex.setPrvd_man_no(area.getCreate_man_no());
						annex.setPrvd_man_name(area.getCreate_man_name());
						annex.setPrvd_time(area.getCreate_time());
						
						if(annex.getAnnex_path()==null){
							rm.setState("500");
							rm.setMsgInfo("请传入附件路径");
							return rm;
						}
						if(!annex.getAnnex_path().contains("group1/M00")){
							annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
						}
						
						this.getMapper(SlmCompanyNewAnnexMapper.class).insert(annex);
					}
				}
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setMsgInfo("该数据状态不允许进行修改");
			}
		}else{
			rm.setMsgInfo("该数据不存在");
			rm.setState("500");
		}

		} catch (Exception e) {
			//--19-10-15添加异常回滚
			throw new Exception("修改失败！"+e.getMessage());
		}
		return rm;
	}
	
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<SlmCompanyNew> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();
		try {
		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sp=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if(sp!=null&&("1".equals(sp.getApproval_flag()))){
				this.getMapper(SlmCompanyNewMapper.class).delete(sp.getCompany_new_no());
				SlmCompanyNewAnnex annex=new SlmCompanyNewAnnex();
				annex.setCompany_new_no(sp.getCompany_new_no());
				List<SlmCompanyNewAnnex> lpa=this.getMapper(SlmCompanyNewAnnexMapper.class).find(annex, 0, 0);
				for(SlmCompanyNewAnnex spa:lpa){
					try{
						//File file = new File(PropertiesUtils.getImageProperties().getProperty("FileUrl")+spa.getAnnex_path());
						//file.delete();
						this.getBean(SpeOptBaseService.class).doDelete(spa.getAnnex_path());
					}catch(Exception ex){}
					
					this.getMapper(SlmCompanyNewAnnexMapper.class).delete(spa.getAnnex_no());
				}
			}
		}
		rm.setState("200");
		
	} catch (Exception e) {
		//--19-10-15添加异常回滚
		throw new Exception("删除失败！"+e.getMessage());
	}
		return rm;
	}

	/**
	 * 启用/废除
	 * memo1为 valid_flag的值
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doStatusFlagWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sp=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if(sp!=null&&("1".equals(sp.getApproval_flag()))){
				area.setValid_flag(sum.getMemo1());
				this.getMapper(SlmCompanyNewMapper.class).update(area);
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 提交
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doSubmitWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if(sppc!=null && "1".equals(sppc.getValid_flag()) &&"1".equals(sppc.getApproval_flag())){
				SlmCompanyNew spp=new SlmCompanyNew();
				spp.setCompany_new_no(area.getCompany_new_no());
				spp.setApproval_flag("2");
				spp.setSubmit_man_no(userId);
				spp.setSubmit_man_name(userName);
				spp.setSubmit_time(new Date());
				this.getMapper(SlmCompanyNewMapper.class).update(spp);
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 撤销提交
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doUnSubmitWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if(sppc!=null&&"1".equals(sppc.getValid_flag()) && "2".equals(sppc.getApproval_flag())){
				area.setApproval_flag("1");
				this.getMapper(SlmCompanyNewMapper.class).doUnSubmit(area.getCompany_new_no());
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 3审核通过，4审核不通过
	 * memo1为 approval_flag的值  1编辑，2提交，3审核通过，4审核不通过，5发布
	 * memo2为评审意见
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doApprovalWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if("2".equals(sppc.getApproval_flag())&&"1".equals(sppc.getValid_flag())){
				SlmCompanyNew spp=new SlmCompanyNew();
				spp.setCompany_new_no(area.getCompany_new_no());
				spp.setApproval_flag(sum.getMemo1());
				spp.setApproval_opinion(sum.getMemo2());
				spp.setApproval_man_no(userId);
				spp.setApproval_man_name(userName);
				spp.setApproval_time(new Date());
				this.getMapper(SlmCompanyNewMapper.class).update(spp);
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 审核撤销
	 * memo1为 approval_flag的值  1编辑，2提交，3审核通过，4审核不通过，5发布
	 * memo2为评审意见
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doUnApprovalWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if(("3".equals(sppc.getApproval_flag())||"4".equals(sppc.getApproval_flag()))&&"1".equals(sppc.getValid_flag())){
				area.setApproval_flag("2");
				this.getMapper(SlmCompanyNewMapper.class).doUnApproval(area.getCompany_new_no());
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 发布
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doReleaseWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if("3".equals(sppc.getApproval_flag())&&"1".equals(sppc.getValid_flag())){
				SlmCompanyNew spp=new SlmCompanyNew();
				spp.setCompany_new_no(area.getCompany_new_no());
				spp.setRelease_man_no(userId);
				spp.setRelease_man_name(userName);
				spp.setApproval_flag("5");
				spp.setRelease_time(new Date());
				this.getMapper(SlmCompanyNewMapper.class).update(spp);
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 撤销发布
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doUnReleaseWeb(CommonPage<SlmCompanyNew> sum){
		ResultModel rm=new ResultModel();
		SlmCompanyNew areas=sum.getObject();
		List<SlmCompanyNew> area_list=sum.getObjectList();

		if(area_list==null){
			area_list=new ArrayList<SlmCompanyNew>();
			area_list.add(areas);
		}
		for(SlmCompanyNew area:area_list){
			SlmCompanyNew sppc=this.getMapper(SlmCompanyNewMapper.class).findById(area.getCompany_new_no());
			if("5".equals(sppc.getApproval_flag())&&"1".equals(sppc.getValid_flag())){
				area.setApproval_flag("3");
				this.getMapper(SlmCompanyNewMapper.class).doUnRelease(area.getCompany_new_no());
			}
		}
		rm.setState("200");
		return rm;
	}
}
