/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-08-01
 * 描述            ：SERIAL_NO
 * 注意事项     ：
 * 遗留BUG   ：            检修任务计划子表
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Repair.impl;
import java.util.ArrayList;
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.PlnRepairOverhaulService;
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="PlnRepairOverhaulService")
public class PlnRepairOverhaulServiceImpl extends BaseServiceImpl<PlnRepairOverhaul,Object> implements PlnRepairOverhaulService {
    /**
     *  添加检修任务计划子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel addPlnRepairOverhaul(CommonPage<PlnRepairOverhaul> sum){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairOverhaul> pro=sum.getObjectList();
            for (PlnRepairOverhaul pln : pro) {
                String overhaul_no="1";
                List<PlnRepairOverhaul> ppp = this.getMapper(PlnRepairOverhaulMapper.class).findWithCondition(null, null, " nvl(to_number(overhaul_no),0) desc", 0, 1);
                if (Utils.isNotEmpty(ppp)) {
                    overhaul_no = Integer.parseInt(ppp.get(0).getOverhaul_no()) + 1+"";
                }
                    pln.setOverhaul_no(overhaul_no);
                    pln.setSerial_no(pro.get(0).getSerial_no());
                    pln.setOverhaul_content(pro.get(0).getOverhaul_content());
                    pln.setDuty_name(pro.get(0).getDuty_name());
                    pln.setSecuse_duty_name(pro.get(0).getSecuse_duty_name());
                    pln.setSkill_duty_name(pro.get(0).getSkill_duty_name());
                    pln.setReoair_class(pro.get(0).getReoair_class());
                    pln.setSpare_case(pro.get(0).getSpare_case());
                    pln.setUnits_name(pro.get(0).getUnits_name());
                    pln.setAchieve(pro.get(0).getAchieve());
                    pln.setNot_achieve(pro.get(0).getNot_achieve());
                this.getMapper(PlnRepairOverhaulMapper.class).insert(pln);
                CommonPage<PlnRepairDetails> commonPage=new CommonPage<PlnRepairDetails>();
                List<PlnRepairDetails> list=new ArrayList<PlnRepairDetails>();
                //根据任务id 查询检修任务实绩的编号，从而当添加检修明细的实绩的时候 将值赋值进去 方便 检修任务实绩查询
                List<PlnRepairPerformance> prpL = this.getMapper(PlnRepairPerformanceMapper.class).findWithCondition(null, "serial_no='"+pro.get(0).getSerial_no()+"'", null, 0, 30);
                if(prpL.size()!=1) {
                	 for (PlnRepairPerformance plnRepairPerformance : prpL) {
                     	for (int i = 0; i <= prpL.size(); i++) {
                     		 PlnRepairDetails plnRepairDetails=new PlnRepairDetails();
                     		plnRepairDetails.setDetatls_performance_no(plnRepairPerformance.getPerformance_no());
                       	  	plnRepairDetails.setSerial_no(pro.get(0).getSerial_no());
                             plnRepairDetails.setOverhaul_content(pln.getOverhaul_content());
                             plnRepairDetails.setOwner_man(pln.getDuty_name());
                             plnRepairDetails.setCustodian_man(pro.get(0).getSecuse_duty_name());
                             plnRepairDetails.setCreate_man_name(pro.get(0).getSkill_duty_name());
                             plnRepairDetails.setReoair_class(pro.get(0).getReoair_class());
                             plnRepairDetails.setSpare_case(pro.get(0).getSpare_case());
                             plnRepairDetails.setUnits_name(pro.get(0).getUnits_name());
                             plnRepairDetails.setAchieve(pro.get(0).getAchieve());
                             plnRepairDetails.setNot_achieve(pro.get(0).getNot_achieve());
                             list.add(plnRepairDetails);
     					}
     				}   
                }else if(prpL.size()==1){
                	for (PlnRepairPerformance plnRepairPerformance : prpL) {
                     		 PlnRepairDetails plnRepairDetails=new PlnRepairDetails();
                     		plnRepairDetails.setDetatls_performance_no(plnRepairPerformance.getPerformance_no());
                       	  	plnRepairDetails.setSerial_no(pro.get(0).getSerial_no());
                             plnRepairDetails.setOverhaul_content(pln.getOverhaul_content());
                             plnRepairDetails.setOwner_man(pln.getDuty_name());
                             plnRepairDetails.setCustodian_man(pro.get(0).getSecuse_duty_name());
                             plnRepairDetails.setCreate_man_name(pro.get(0).getSkill_duty_name());
                             plnRepairDetails.setReoair_class(pro.get(0).getReoair_class());
                             plnRepairDetails.setSpare_case(pro.get(0).getSpare_case());
                             plnRepairDetails.setUnits_name(pro.get(0).getUnits_name());
                             plnRepairDetails.setAchieve(pro.get(0).getAchieve());
                             plnRepairDetails.setNot_achieve(pro.get(0).getNot_achieve());
                             list.add(plnRepairDetails);
     					}
                }
                commonPage.setObjectList(list);
                this.getBean(PlnRepairDetailsServiceImpl.class).addPlnRepairDetails(commonPage,overhaul_no);
                }
        } 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 deletePlnRepairOverhaul(CommonPage<PlnRepairOverhaul> sum){
        ResultModel rm=new ResultModel();
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairOverhaul> pro=sum.getObjectList();
                for (PlnRepairOverhaul target: pro) {
                    //刪除检修任务子表
                        List<PlnRepairDetails> proList = this.getMapper(PlnRepairDetailsMapper.class).findWithCondition(null, "overhaul_no = '"+target.getOverhaul_no()+"'", null, 0, 1);
                        if(proList.size()==0){
                            this.getMapper(PlnRepairOverhaulMapper.class).delete(target.getOverhaul_no());
                            rm.setState("200");
                            rm.setMsgInfo("删除成功！");
                        }else{
                            throw new Exception("任务详情编号["+target.getOverhaul_no()+"]存在检修实绩！无法操作！");
                        }
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }

    /**
     *  修改检修任务计划子表信息
     * @param sum
     * @return
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel updatePlnRepairOverhaul(CommonPage<PlnRepairOverhaul> sum){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairOverhaul> pro=sum.getObjectList();
            for (PlnRepairOverhaul pln : pro) {
                pln.setOverhaul_content(pro.get(0).getOverhaul_content());
                pln.setReoair_class(pro.get(0).getReoair_class());
                pln.setSpare_case(pro.get(0).getSpare_case());
                pln.setUnits_name(pro.get(0).getUnits_name());
                pln.setAchieve(pro.get(0).getAchieve());
                pln.setNot_achieve(pro.get(0).getNot_achieve());
                pln.setSkill_duty_name(pro.get(0).getSkill_duty_name());
                pln.setSecuse_duty_name(pro.get(0).getSecuse_duty_name());
                pln.setDuty_name(pro.get(0).getDuty_name());
                this.getMapper(PlnRepairOverhaulMapper.class).update(pln);
                CommonPage<PlnRepairDetails> commonPage=new CommonPage<PlnRepairDetails>();
                List<PlnRepairDetails> list=new ArrayList<PlnRepairDetails>();
                PlnRepairDetails plnRepairDetails=new PlnRepairDetails();
                plnRepairDetails.setSerial_no(pro.get(0).getSerial_no());
                plnRepairDetails.setOverhaul_content(pro.get(0).getOverhaul_content());
                plnRepairDetails.setAchieve(pro.get(0).getAchieve());
                plnRepairDetails.setNot_achieve(pro.get(0).getNot_achieve());
                plnRepairDetails.setOverhaul_no(pro.get(0).getOverhaul_no());
                plnRepairDetails.setReoair_class(pro.get(0).getReoair_class());
                plnRepairDetails.setSpare_case(pro.get(0).getSpare_case());
                plnRepairDetails.setUnits_name(pro.get(0).getUnits_name());
                plnRepairDetails.setOwner_man(pro.get(0).getDuty_name());
                plnRepairDetails.setCustodian_man(pro.get(0).getSecuse_duty_name());
                plnRepairDetails.setCreate_man_name(pro.get(0).getSkill_duty_name());
                plnRepairDetails.setDetails_true(sum.getMemo1());
                list.add(plnRepairDetails);
                commonPage.setObjectList(list);
                this.getBean(PlnRepairDetailsServiceImpl.class).updatePlnRepairDetails(commonPage);
            }
        } 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 queryPlnRepairOverhaul(CommonPage<PlnRepairOverhaul> sum){
        ResultModel rm=new ResultModel("200", null);
        if (Utils.isEmpty(sum)) {
            rm.setState("500");
            rm.setMsgInfo("请传入数据！");
            return rm;
        }
        try {
            List<PlnRepairOverhaul> pro=sum.getObjectList();
            for (PlnRepairOverhaul prov: pro) {
                List<PlnRepairOverhaul> proList = this.getMapper(PlnRepairOverhaulMapper.class).findWithCondition(null, "serial_no  = '"+prov.getSerial_no()+"'", "overhaul_no", 0, 30);
                rm.setData(proList);
            }
        } catch (Exception ex) {
            rm.setState("500");
            rm.setMsgInfo("查询检修任务计划子表信息失败!");
            rm.setErrorInfo(ex.getMessage());
        }
        return rm;
    }


}
