package com.fastbee.xunjian.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.SecurityUtils;
import com.fastbee.xunjian.domain.XjInspectionRecords;
import com.fastbee.xunjian.mapper.XjInspectionRecordsMapper;
import com.fastbee.xunjian.service.IXjInspectionRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 记录Service业务层处理
 *
 * @author kenlixunjian
 * @date 2023-08-04
 */
@Service
public class XjInspectionRecordsServiceImpl implements IXjInspectionRecordsService {
    @Autowired
    private XjInspectionRecordsMapper XjInspectionRecordsMapper;

    /**
     * 查询记录
     *
     * @param id 记录主键
     * @return 记录
     */
    @Override
    public XjInspectionRecords selectXjInspectionRecordsById(Long id) {
        XjInspectionRecords XjInspectionRecords = XjInspectionRecordsMapper.selectXjInspectionRecordsById(id);
        return XjInspectionRecords;
    }

    /**
     * 查询记录列表
     *
     * @param XjInspectionRecords 记录
     * @return 记录
     */
    @Override
    public List<XjInspectionRecords> selectXjInspectionRecordsList(XjInspectionRecords XjInspectionRecords) {
//        try {
//            if (SecurityUtils.getLoginUser().getUser().isZuzhang()) {
//                KlxjInspectionPersonnels klxjInspectionPersonnels = SecurityUtils.getLoginUser().getPersonnels();
//                if (klxjInspectionPersonnels != null) {
//                    XjInspectionRecords.setGroupId(klxjInspectionPersonnels.getGroupId());
//                }
//            } else if (SecurityUtils.getLoginUser().getUser().isXunjianyuan()) {
//                KlxjInspectionPersonnels klxjInspectionPersonnels = SecurityUtils.getLoginUser().getPersonnels();
//                if (klxjInspectionPersonnels != null) {
//                    XjInspectionRecords.setInspectionPersonnelId(klxjInspectionPersonnels.getId());
//                }
//            }
//        } catch (Exception a) {
//        }
        List<XjInspectionRecords> XjInspectionRecords1 =
                XjInspectionRecordsMapper.selectXjInspectionRecordsList(XjInspectionRecords);

//        for (XjInspectionRecords inspectionRecords : XjInspectionRecords1) {
//            List<KlxjInspectionEvents> list = inspectionEventsService.selectKlxjInspectionEventsList(new KlxjInspectionEvents() {{
//                setInspectionRecordId(inspectionRecords.getId());
//            }});
//            if (list != null && list.size() > 0) {
//                inspectionRecords.setAbnormalType(list.get(0).getAbnormalType());
//            }
//        }

        return XjInspectionRecords1;
    }

    /**
     * 新增记录
     *
     * @param XjInspectionRecords 记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertXjInspectionRecords(XjInspectionRecords XjInspectionRecords) {
        XjInspectionRecords.setCreateTime(DateUtils.getNowDate());
//        //执行人
//        KlxjInspectionPersonnels klxjInspectionPersonnels =
//                inspectionPersonnelsService.selectKlxjInspectionPersonnelsById(XjInspectionRecords.getInspectionPersonnelId());
//        XjInspectionRecords.setGroupId(klxjInspectionPersonnels.getGroupId());
//        if (XjInspectionRecords.getInspectionRouteId() != null) {
//            KlxjInspectionTasks klxjInspectionTasks = new KlxjInspectionTasks();
//            klxjInspectionTasks.setParams(new HashMap<String, Object>() {{
//                put("taskTime", DateUtil.date().toString());
//            }});
//            klxjInspectionTasks.setInspectionPersonnelId(XjInspectionRecords.getInspectionPersonnelId());
//            List<KlxjInspectionTasks> klxjInspectionTasks1 = inspectionTasksService.selectKlxjInspectionTasksList(klxjInspectionTasks);
//            if (klxjInspectionTasks1 != null) {
//                for (KlxjInspectionTasks inspectionTasks : klxjInspectionTasks1) {
//                    if (XjInspectionRecords.getInspectionTaskId() != null && XjInspectionRecords.getInspectionTaskId() != 0) {
//                        break;
//                    }
//                    for (KlxjInspectionRoutes klxjInspectionRoutes : inspectionTasks.getInspectionRouteList()) {
//                        if (klxjInspectionRoutes.getId().equals(XjInspectionRecords.getInspectionRouteId())) {
//                            XjInspectionRecords.setInspectionPlanId(inspectionTasks.getInspectionPlanId());
//                            XjInspectionRecords.setInspectionTaskId(inspectionTasks.getId());
//                            break;
//                        }
//                    }
//                }
//            }
//        }
//        if (XjInspectionRecords.getInspectionTaskId() != null && XjInspectionRecords.getInspectionTaskId() != 0) {
//            KlxjInspectionTasks klxjInspectionTasks = inspectionTasksService.selectKlxjInspectionTasksById(XjInspectionRecords.getInspectionTaskId());
//
//
//            if (klxjInspectionTasks != null && klxjInspectionTasks.getInspectionRouteIds() != null && !klxjInspectionTasks.getInspectionRouteIds().equals("")) {
//
//                List<Long> collect = klxjInspectionTasks.getCompletedInspectionRouteList().stream().map(t -> t.getId()).collect(Collectors.toList());
//                if (!collect.contains(XjInspectionRecords.getInspectionRouteId())) {
//                    String completedInspectionRouteIds = klxjInspectionTasks.getCompletedInspectionRouteIds() == null || klxjInspectionTasks.getCompletedInspectionRouteIds().equals("")
//                            ? "" + XjInspectionRecords.getInspectionRouteId()
//                            : klxjInspectionTasks.getCompletedInspectionRouteIds() + "," + XjInspectionRecords.getInspectionRouteId();
//                    klxjInspectionTasks.setCompletedInspectionRouteIds(completedInspectionRouteIds);
//                    if (!klxjInspectionTasks.getTaskStatus().equals("4")) {
//                        if (klxjInspectionTasks.getInspectionRouteIds().split(",").length == klxjInspectionTasks.getCompletedInspectionRouteIds().split(",").length) {
//                            klxjInspectionTasks.setTaskStatus("3");
//                        } else {
//                            klxjInspectionTasks.setTaskStatus("2");
//                        }
//                    }
//                }
//                inspectionTasksService.updateKlxjInspectionTasks(klxjInspectionTasks);
//            }
//        }
        return XjInspectionRecordsMapper.insertXjInspectionRecords(XjInspectionRecords);
    }

    /**
     * 修改记录
     *
     * @param XjInspectionRecords 记录
     * @return 结果
     */
    @Override
    public int updateXjInspectionRecords(XjInspectionRecords XjInspectionRecords) {
        XjInspectionRecords.setUpdateTime(DateUtils.getNowDate());
//        KlxjInspectionPersonnels klxjInspectionPersonnels = inspectionPersonnelsService.selectKlxjInspectionPersonnelsById(XjInspectionRecords.getInspectionPersonnelId());
//        XjInspectionRecords.setGroupId(klxjInspectionPersonnels.getGroupId());
//        if (XjInspectionRecords.getInspectionRouteId() != null) {
//            KlxjInspectionTasks klxjInspectionTasks = new KlxjInspectionTasks();
//            klxjInspectionTasks.setParams(new HashMap<String, Object>() {{
//                put("taskTime", DateUtil.date().toString());
//            }});
//            klxjInspectionTasks.setInspectionPersonnelId(XjInspectionRecords.getInspectionPersonnelId());
//            List<KlxjInspectionTasks> klxjInspectionTasks1 = inspectionTasksService.selectKlxjInspectionTasksList(klxjInspectionTasks);
//            if (klxjInspectionTasks1 != null) {
//                for (KlxjInspectionTasks inspectionTasks : klxjInspectionTasks1) {
//                    if (XjInspectionRecords.getInspectionTaskId() != null && XjInspectionRecords.getInspectionTaskId() != 0) {
//                        break;
//                    }
//                    for (KlxjInspectionRoutes klxjInspectionRoutes : inspectionTasks.getInspectionRouteList()) {
//                        if (klxjInspectionRoutes.getId().equals(XjInspectionRecords.getInspectionRouteId())) {
//                            XjInspectionRecords.setInspectionPlanId(inspectionTasks.getInspectionPlanId());
//                            XjInspectionRecords.setInspectionTaskId(inspectionTasks.getId());
//                            break;
//                        }
//                    }
//                }
//            }
//        }
//        if (XjInspectionRecords.getInspectionTaskId() != null && XjInspectionRecords.getInspectionTaskId() != 0) {
//            KlxjInspectionTasks klxjInspectionTasks = inspectionTasksService.selectKlxjInspectionTasksById(XjInspectionRecords.getInspectionTaskId());
//
//
//            if (klxjInspectionTasks != null && klxjInspectionTasks.getInspectionRouteIds() != null && !klxjInspectionTasks.getInspectionRouteIds().equals("")) {
//
//                List<Long> collect = klxjInspectionTasks.getCompletedInspectionRouteList().stream().map(t -> t.getId()).collect(Collectors.toList());
//                if (!collect.contains(XjInspectionRecords.getInspectionRouteId())) {
//                    klxjInspectionTasks.setCompletedInspectionRouteIds(klxjInspectionTasks.getCompletedInspectionRouteIds() == null
//                            ? "" + XjInspectionRecords.getInspectionRouteId()
//                            : klxjInspectionTasks.getCompletedInspectionRouteIds() + "," + XjInspectionRecords.getInspectionRouteId());
//                    if (!klxjInspectionTasks.getTaskStatus().equals("4")) {
//                        if (klxjInspectionTasks.getCompletedInspectionRouteIds().split(",").length == klxjInspectionTasks.getCompletedInspectionRouteIds().split(",").length) {
//                            klxjInspectionTasks.setTaskStatus("3");
//                        } else {
//                            klxjInspectionTasks.setTaskStatus("2");
//                        }
//                    }
//                }
//                inspectionTasksService.updateKlxjInspectionTasks(klxjInspectionTasks);
//            }
//        }
        return XjInspectionRecordsMapper.updateXjInspectionRecords(XjInspectionRecords);
    }

    /**
     * 批量删除记录
     *
     * @param ids 需要删除的记录主键
     * @return 结果
     */
    @Override
    public int deleteXjInspectionRecordsByIds(Long[] ids) {
        return XjInspectionRecordsMapper.deleteXjInspectionRecordsByIds(ids);
    }

    /**
     * 删除记录信息
     *
     * @param id 记录主键
     * @return 结果
     */
    @Override
    public int deleteXjInspectionRecordsById(Long id) {
        return XjInspectionRecordsMapper.deleteXjInspectionRecordsById(id);
    }

    @Override
    public List<HashMap> getInspectionTasksYichangCountByParmas(HashMap<String, Object> params) {
        return XjInspectionRecordsMapper.getInspectionTasksYichangCountByParmas(params);
    }

//    @Override
//    public List<XjInspectionRecords> listByGroupName(KlxjInspectiontargetGroup
//                                                                   klxjInspectiontargetGroup) {
//        KlxjInspectiontargetGroup klxjInspectiontargetGroup1 =
//                klxjInspectiontargetGroupMapper.selectByName(klxjInspectiontargetGroup.getName());
//        if(null == klxjInspectiontargetGroup1){
//            return  new ArrayList<XjInspectionRecords>();
//        }
//        if(StringUtils.isEmpty(klxjInspectiontargetGroup1.getRoutesIds())){
//            return  new ArrayList<XjInspectionRecords>();
//        }
//        klxjInspectiontargetGroup.setRoutesIds(klxjInspectiontargetGroup1.getRoutesIds());
//        if(StringUtils.isEmpty(klxjInspectiontargetGroup.getEndTime())){
//            klxjInspectiontargetGroup.setEndTime(DateUtils.getTime());
//        }
//        if(StringUtils.isEmpty(klxjInspectiontargetGroup.getBeginTime())){
//            Calendar calendar = Calendar.getInstance();
//            int year = calendar.get(Calendar.YEAR);
////            calendar.add(Calendar.DATE,-30);
////            Date beginDate = calendar.getTime();
//            klxjInspectiontargetGroup.setBeginTime(year + "-01-01 00:00:00");
//        }
//        PageUtils.startPage();
//        List<XjInspectionRecords> XjInspectionRecords = XjInspectionRecordsMapper.selectListByRoutesIds(klxjInspectiontargetGroup);
//        return XjInspectionRecords;
//    }
}
