/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-08-01
 * 描述            ：SERIAL_NO
 * 注意事项     ：
 * 遗留BUG   ：         检修计划实绩子表
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Repair.impl;
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.PlnRepairDetailsMapper;
import market.Mes.Repair.mapper.PlnRepairOverhaulMapper;
import market.Mes.Repair.mapper.PlnRepairPerformanceMapper;
import market.Mes.Repair.service.PlnRepairDetailsService;
import market.Mes.Repair.vo.PlnRepairDetails;
import market.Mes.Repair.vo.PlnRepairOverhaul;
import market.Mes.Repair.vo.PlnRepairPerformance;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="PlnRepairDetailsService")
public class PlnRepairDetailsServiceImpl extends BaseServiceImpl<PlnRepairDetails,Object> implements PlnRepairDetailsService {
    /**
     * 查询检修计划实绩子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel queryPlnRepairDetails(CommonPage<PlnRepairDetails> sum){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairDetails> pro=sum.getObjectList();
            for (PlnRepairDetails prdv: pro) {
            	if(prdv.getDetatls_performance_no()!=null) {
            		 List<PlnRepairDetails> proListPRD = this.getMapper(PlnRepairDetailsMapper.class).findWithCondition(null, "serial_no='"+prdv.getSerial_no()+"' and detatls_performance_no='"+prdv.getDetatls_performance_no()+"'", "detatls_no", 0, 30);
            		  rm.setData(proListPRD);
            	}else {
            		 List<PlnRepairDetails> proListPRD = this.getMapper(PlnRepairDetailsMapper.class).findWithCondition(null, "serial_no='"+prdv.getSerial_no()+"'", "detatls_no", 0, 30);
            		 rm.setData(proListPRD);
            	}  
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setMsgInfo("查询检修计划实绩子表信息失败!");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }
    /**
     * 添加检修计划实绩子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel addPlnRepairDetails(CommonPage<PlnRepairDetails> sum,String overhaul_no){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairDetails> objectList = sum.getObjectList();
            for (PlnRepairDetails prd2: objectList) {
                String detatls_no="1";
                List<PlnRepairDetails> ppp = this.getMapper(PlnRepairDetailsMapper.class).findWithCondition(null, null, " nvl(to_number(detatls_no),0) desc", 0, 1);
                if (Utils.isNotEmpty(ppp)) {
                    detatls_no = Integer.parseInt(ppp.get(0).getDetatls_no()) + 1+"";
                }
                List<PlnRepairOverhaul> proList = this.getMapper(PlnRepairOverhaulMapper.class).findWithCondition(null, "overhaul_no  = '"+overhaul_no+"'", null, 0, 1);
                prd2.setDetatls_no(detatls_no);
                prd2.setSerial_no(proList.get(0).getSerial_no());//任务编号
                prd2.setOverhaul_content(proList.get(0).getOverhaul_content());
                prd2.setOwner_man(proList.get(0).getDuty_name());
                prd2.setCustodian_man(proList.get(0).getSecuse_duty_name());
                prd2.setCreate_man_name(proList.get(0).getSkill_duty_name());
                prd2.setReoair_class(proList.get(0).getReoair_class());
                prd2.setSpare_case(proList.get(0).getSpare_case());
                prd2.setUnits_name(proList.get(0).getUnits_name());
                prd2.setAchieve(proList.get(0).getAchieve());
                prd2.setNot_achieve(proList.get(0).getNot_achieve());
                prd2.setOverhaul_no(overhaul_no);
                this.getMapper(PlnRepairDetailsMapper.class).insert(prd2);
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setMsgInfo("添加检修计划实绩子表信息失败!");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }


    /**
     * 修改检修计划实绩子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel updatePlnRepairDetails(CommonPage<PlnRepairDetails> sum){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairDetails> objectList = sum.getObjectList();
            for (PlnRepairDetails prd2: objectList) {        	  
                    if("1".equals(prd2.getDetails_true())) {
                    	 prd2.setDetatls_no(objectList.get(0).getDetatls_no());
                    	 prd2.setOverhaul_no(objectList.get(0).getOverhaul_no());
                         prd2.setSerial_no(objectList.get(0).getSerial_no());//任务编号
                    	 //prd2.setAchieve(sum.getMemo5());
                         prd2.setAchieve(objectList.get(0).getAchieve());
                      /* prd2.setNot_achieve(sum.getMemo6());
                         prd2.setRemark(sum.getMemo7());//备注	*/    
                         prd2.setNot_achieve(objectList.get(0).getNot_achieve());
                         prd2.setRemark(objectList.get(0).getRemark());//备注	
                      }else if("2".equals(prd2.getDetails_true())){
	                      prd2.setOverhaul_no(objectList.get(0).getOverhaul_no());
	                      prd2.setSerial_no(objectList.get(0).getSerial_no());//任务编号
	                      prd2.setOverhaul_content(objectList.get(0).getOverhaul_content());//检修内容
	                      prd2.setAchieve(objectList.get(0).getAchieve());//完成情况
	                      prd2.setNot_achieve(objectList.get(0).getNot_achieve());//未完成的原因
	                      prd2.setReoair_class(objectList.get(0).getReoair_class());
	                      prd2.setSpare_case(objectList.get(0).getSpare_case());
	                      prd2.setUnits_name(objectList.get(0).getUnits_name());
	                      prd2.setOwner_man(objectList.get(0).getOwner_man());
	                      prd2.setCustodian_man(objectList.get(0).getCustodian_man());
	                      prd2.setCreate_man_name(objectList.get(0).getCreate_man_name());
	                      List<PlnRepairPerformance> ppp = this.getMapper(PlnRepairPerformanceMapper.class).findWithCondition(null, "serial_no  = '"+prd2.getSerial_no()+"'", " nvl(to_number(task_id),0) desc", 0, 1);
	                      prd2.setTask_id(ppp.get(0).getTask_id());
	//   					prd2.setOwner_man(proList.get(0).getDuty_name());
	//                    prd2.setCustodian_man(proList.get(0).getSecuse_duty_name());
	//                    prd2.setCreate_man_name(proList.get(0).getSkill_duty_name());
	//   					prd.setCreate_man_name(sum.getMemo1());//填报人
	//                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	//                    Date dateStart = formatter.parse(sum.getStartTime());
	//                    Date dateEnd = formatter.parse(sum.getEndTime());
	//                    prd.setCreate_time(dateStart);//创建时间
	//                    prd.setTask_begin_time(dateStart);//任务开始时间
	//                    prd.setTask_end_time(dateEnd);//任务结束时间
	//                    prd.setRepair_reason(prd2.getRepair_reason());//检修原因
	//                    prd.setOwner_man(prd2.getOwner_man());//监督人
	                      prd2.setRemark(objectList.get(0).getRemark());//备注     
                    } 
                    this.getMapper(PlnRepairDetailsMapper.class).update(prd2);
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setMsgInfo("修改检修计划实绩子表信息失败!");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }
    /**
     *  刪除检修实绩子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel deletePlnRepairDetails(CommonPage<PlnRepairDetails> sum){
        ResultModel rm=new ResultModel();
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairDetails> pro=sum.getObjectList();
            for (PlnRepairDetails target: pro) {
                    this.getMapper(PlnRepairDetailsMapper.class).delete(target.getDetatls_no(),target.getSerial_no(),target.getOverhaul_no());
                    rm.setState("200");
                    rm.setMsgInfo("删除成功！");
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }

}
