package com.ruoyi.inspection.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.inspection.domain.VoReturnInspectionRecordTable;
import com.ruoyi.inspection.domain.VoViewInspectionRecordTable;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inspection.mapper.InspectionRecordTableMapper;
import com.ruoyi.inspection.domain.InspectionRecordTable;
import com.ruoyi.inspection.service.IInspectionRecordTableService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 巡检记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Service
public class InspectionRecordTableServiceImpl implements IInspectionRecordTableService {
    @Autowired
    private InspectionRecordTableMapper inspectionRecordTableMapper;

    @Autowired
    private InspectionManageTableServiceImpl inspectionManageTableService;

    //巡检类型
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询巡检记录
     *
     * @param id 巡检记录主键
     * @return 巡检记录
     */
    @Override
    public InspectionRecordTable selectInspectionRecordTableById(Long id) {
        return inspectionRecordTableMapper.selectInspectionRecordTableById(id);
    }

    /**
     * 查询巡检记录列表
     *
     * @param inspectionRecordTable 巡检记录
     * @return 巡检记录
     */
    @Override
    @DataScope(userAlias = "u")
    public List<InspectionRecordTable> selectInspectionRecordTableList(InspectionRecordTable inspectionRecordTable) {
        return inspectionRecordTableMapper.selectInspectionRecordTableList(inspectionRecordTable);
    }

    /**
     * 新增巡检记录
     *
     * @param inspectionRecordTable 巡检记录
     * @return 结果
     */
    @Override
    public int insertInspectionRecordTable(InspectionRecordTable inspectionRecordTable) {
        inspectionRecordTable.setCreateBy(SecurityUtils.getUsername());
        inspectionRecordTable.setCreateTime(DateUtils.getNowDate());
        if (inspectionRecordTable.getInspectionTime() == null) {
            inspectionRecordTable.setInspectionTime(DateUtils.getNowDate());
        }
        return inspectionRecordTableMapper.insertInspectionRecordTable(inspectionRecordTable);
    }

    /**
     * 修改巡检记录
     *
     * @param inspectionRecordTable 巡检记录
     * @return 结果
     */
    @Override
    public int updateInspectionRecordTable(InspectionRecordTable inspectionRecordTable) {
        inspectionRecordTable.setUpdateBy(SecurityUtils.getUsername());
        inspectionRecordTable.setUpdateTime(DateUtils.getNowDate());
        return inspectionRecordTableMapper.updateInspectionRecordTable(inspectionRecordTable);
    }

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

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

    /**
     * 查询巡检记录
     *
     * @param voViewInspectionRecordTable 巡检记录
     * @return 巡检记录
     */
    @Override
    public VoReturnInspectionRecordTable selectInspectionRecordView(VoViewInspectionRecordTable voViewInspectionRecordTable) {
        VoViewInspectionRecordTable voViewInspectionRecordTable1 = handleWeekType(voViewInspectionRecordTable);
        List<InspectionRecordTable> tables = inspectionRecordTableMapper.selectInspectionRecordViewList(voViewInspectionRecordTable1);
        int count = inspectionRecordTableMapper.selectInspectionRecordViewCount(voViewInspectionRecordTable1);
        VoReturnInspectionRecordTable voReturnInspectionRecordTable = new VoReturnInspectionRecordTable();
        voReturnInspectionRecordTable.setInspectionRecordTables(tables);
        voReturnInspectionRecordTable.setClockIn(count);
        return voReturnInspectionRecordTable;
    }

    /**
     * 处理周类型
     *
     * @param voViewInspectionRecordTable
     * @return
     */
    private VoViewInspectionRecordTable handleWeekType(VoViewInspectionRecordTable voViewInspectionRecordTable) {
        VoViewInspectionRecordTable Table = new VoViewInspectionRecordTable();
        switch (voViewInspectionRecordTable.getWeekType()) {
            case 0:
                Table.setStartNum(0);
                Table.setEndNum(0);
                break;
            case 1:
                Table.setStartNum(1);
                Table.setEndNum(7);
                break;
            case 2:
                Table.setStartNum(8);
                Table.setEndNum(15);
                break;
            case 3:
                Table.setStartNum(16);
                Table.setEndNum(23);
                break;
            case 4:
                Table.setStartNum(24);
                Table.setEndNum(31);
                break;
        }
        Table.setMonth(voViewInspectionRecordTable.getMonth());
        Table.setInspectorUser(voViewInspectionRecordTable.getInspectorUser());
        return Table;
    }

    /**
     * 查询巡检记录列表用于导出数据调整
     *
     * @param inspectionRecordTable 巡检记录
     * @return 巡检记录
     */
    @Override
    public List<InspectionRecordTable> selectInspectionRecordTableLists(InspectionRecordTable inspectionRecordTable) {
        List<SysDictData> dictList = sysDictDataMapper.selectDictData();
        List<InspectionRecordTable> list = inspectionRecordTableMapper.selectInspectionRecordTableList(inspectionRecordTable);
        //循环遍历list集合，将集合中的inspectionType(类型为Integer)对应值与dictList集合中dictValue(类型为String)对应的值相比较，如果相等就将dictList集合中dictLabel对应的值赋值给list集合中的inspectionTypeName
        for (InspectionRecordTable inspectionRecordTable1 : list) {
            for (SysDictData sysDictData : dictList) {
                if (sysDictData.getDictValue().equals(inspectionRecordTable1.getInspectionType().toString())) {
                    inspectionRecordTable1.setInspectionTypeName(sysDictData.getDictLabel());
                }
            }
        }
        return list;
    }

}
