/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-10-24
 * 描述            ：SERIAL_NO
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Repair.impl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Mes.impl.Utils;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Repair.mapper.PlnRepairPerformanceMapper;
import market.Mes.Repair.service.PlnRepairPerformanceService;
import market.Mes.Repair.vo.PlnRepairDetails;
import market.Mes.Repair.vo.PlnRepairPerformance;

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="PlnRepairPerformanceService")
public class PlnRepairPerformanceServiceImpl extends BaseServiceImpl<PlnRepairPerformance,Object> implements PlnRepairPerformanceService {
	
	/**
	 * 查询检修实绩
	 * @param sum
	 * @param
	 * @return
	 */
	public ResultModel queryRepairPerformance(CommonPage<PlnRepairPerformance> sum){
		ResultModel rm=new ResultModel("200", null);
		try{
			List<PlnRepairPerformance> prp = new ArrayList<>();
			long count = 0l;
			PageModel<PlnRepairPerformance> pm=new PageModel<PlnRepairPerformance>(sum.getPageIndex(),sum.getPageSize());
			PlnRepairPerformance perf = sum.getObject();
			String sWhere="";
			// 计划检修时间
			if ("1".equals(sum.getMemo1())) {
//				if(Utils.isNotEmpty(sum.getStartTime())){
//					sWhere+=" and prp.task_end_time >= to_date('"+sum.getStartTime()+"','yyyy-MM-dd HH24:mi:ss') ";
//				}
//				if(Utils.isNotEmpty(sum.getEndTime())){
//					sWhere+=" and prp.task_begin_time <= to_date('"+sum.getEndTime()+"','yyyy-MM-dd HH24:mi:ss')";
//				}
				prp = this.getMapper(PlnRepairPerformanceMapper.class).queryRepairPlan(perf, (sum.getPageIndex()-1)*sum.getPageSize(),sum.getPageIndex()*sum.getPageSize(),sum.getStartTime(),sum.getEndTime());
				count=this.getMapper(PlnRepairPerformanceMapper.class).queryRepairPlanCount(sum.getObject(),sum.getStartTime(),sum.getEndTime());

			// 实际开始时间
			}else if ("2".equals(sum.getMemo1())) {
				if(Utils.isNotEmpty(sum.getStartTime())){
					sWhere+=" and temp.repair_begin_time >= to_date('"+sum.getStartTime()+"','yyyy-MM-dd ') ";
				}
				if(Utils.isNotEmpty(sum.getEndTime())){
					sWhere+=" and temp.repair_end_time <= to_date('"+sum.getEndTime()+"','yyyy-MM-dd ')";
				}
				prp = this.getMapper(PlnRepairPerformanceMapper.class).queryperformance(sum.getObject(), sWhere, (sum.getPageIndex()-1)*sum.getPageSize(),sum.getPageIndex()*sum.getPageSize());
				count=this.getMapper(PlnRepairPerformanceMapper.class).queryperformanceCount(sum.getObject(), sWhere);
			}
			pm.setRows(prp);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			ex.printStackTrace();
			rm.setErrorInfo(ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 更新检修实绩
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel updateRepairPerformance(CommonPage<PlnRepairPerformance> sum){
		ResultModel rm=new ResultModel("200", null);
		if (Utils.isEmpty(sum)) {
			rm.setState("500");
			rm.setMsgInfo("请传入数据！");
			return rm;
		}
		List<PlnRepairPerformance> pln = sum.getObjectList();
		try {
			for (PlnRepairPerformance prp : pln) {
				// 更新
				if (Utils.isNotEmpty(prp.getSerial_no()) &&  Utils.isNotEmpty(prp.getTask_id())) {
					this.getMapper(PlnRepairPerformanceMapper.class).update(prp);
					  CommonPage<PlnRepairDetails> commonPage=new CommonPage<PlnRepairDetails>();
		                List<PlnRepairDetails> list=new ArrayList<PlnRepairDetails>();
		                PlnRepairDetails plns=new PlnRepairDetails();
		                plns.setDetatls_no(prp.getDetatls_no());
		                plns.setSerial_no(prp.getSerial_no().toString());
		                plns.setOverhaul_no(prp.getOverhaul_no());
		                plns.setDetails_true(prp.getDetails_true());
		                plns.setOverhaul_content(prp.getOverhaul_content());
		            	 // 完成情况
		                plns.setAchieve(prp.getAchieve());
		            	// 未完成情况
		                plns.setNot_achieve(prp.getNot_Achieve());
		                plns.setRemark(prp.getRemarks());//备注
		                list.add(plns);
		                commonPage.setObjectList(list);
					this.getBean(PlnRepairDetailsServiceImpl.class).updatePlnRepairDetails(commonPage);
					// 新增
				}else {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					int count = this.getMapper(PlnRepairPerformanceMapper.class).countWithCondition(null, "serial_no  = '"+prp.getSerial_no()+"' and plan_time = to_date('"+prp.getPlan()+"','yyyy-MM-dd ') ");
					if (count>1) {
						rm.setState("500");
						rm.setMsgInfo("同一个计划["+prp.getSerial_no()+"],一天只能维护一个!");
						return rm;
					}
					long task_id = 1l;
					List<PlnRepairPerformance> ppp = this.getMapper(PlnRepairPerformanceMapper.class).findWithCondition(null, "serial_no  = '"+prp.getSerial_no()+"'", " nvl(to_number(task_id),0) desc", 0, 1);
					if (Utils.isNotEmpty(ppp)) {
						task_id = ppp.get(0).getTask_id() + 1;
					}
					//添加一个检修任务实绩的自动增长的编号
					 String performance_no="1";
		                List<PlnRepairPerformance> prpL = this.getMapper(PlnRepairPerformanceMapper.class).findWithCondition(null, null, " nvl(to_number(performance_no),0) desc", 0, 1);
		                if (Utils.isNotEmpty(prpL)) {
		                	performance_no =Integer.parseInt(prpL.get(0).getPerformance_no()) + 1+"";
		                }
		            prp.setPerformance_no(performance_no+"");
					prp.setSerial_no(prp.getSerial_no());
					prp.setTask_id(task_id);
					prp.setCreate_time(new Date());
					prp.setPlan_time(format.parse(prp.getPlan()));
					prp.setCreate_man_name((String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
					this.getMapper(PlnRepairPerformanceMapper.class).insert(prp);
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("检修实绩编辑失败!");
			ex.printStackTrace();
			rm.setErrorInfo(ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 删除检修实绩
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel deleteRepairPerformance(CommonPage<PlnRepairPerformance> sum){
		ResultModel rm=new ResultModel("200", null);
		if (Utils.isEmpty(sum)) {
			rm.setState("500");
			rm.setMsgInfo("请传入数据！");
			return rm;
		}
		List<PlnRepairPerformance> prp =sum.getObjectList();
		try {
			for (PlnRepairPerformance pln : prp) {
				if (Utils.isEmpty(pln.getTask_id())) {
					rm.setState("500");
					rm.setMsgInfo("检修实绩不存在,无法删除!");
					return rm;
				}
				this.getMapper(PlnRepairPerformanceMapper.class).delete(pln.getSerial_no(),pln.getTask_id());
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("删除检修实绩失败!");
			ex.printStackTrace();
			rm.setErrorInfo(ex.getMessage());
		}
		return rm;
	}
}
