package com.zd.laboratory.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.zd.common.core.constant.HttpStatus;
import com.zd.common.core.domain.R;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.domain.LabSemester;
import com.zd.laboratory.domain.vo.LabNegativelistVO;
import com.zd.laboratory.domain.vo.LabViolationPageListVO;
import com.zd.laboratory.mapper.LabSemesterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabNegativelistHistoryMapper;
import com.zd.laboratory.domain.LabNegativelistHistory;
import com.zd.laboratory.service.ILabNegativelistHistoryService;

/**
 * 负面清单历史记录Service业务层处理
 *
 * @author zd
 * @date 2021-12-17
 */
@Service
public class LabNegativelistHistoryServiceImpl implements ILabNegativelistHistoryService
{
    @Autowired
    private LabNegativelistHistoryMapper labNegativelistHistoryMapper;

    @Autowired
    private LabSemesterMapper semesterMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询负面清单历史记录
     *
     * @param id 负面清单历史记录主键
     * @return 负面清单历史记录
     */
    @Override
    public LabNegativelistHistory selectLabNegativelistHistoryById(Long id)
    {
        return labNegativelistHistoryMapper.selectLabNegativelistHistoryById(id);
    }

    /**
     * 查询负面清单历史记录列表
     *
     * @param vo 负面清单历史记录
     * @return 负面清单历史记录
     */
    @Override
    public List<LabNegativelistHistory> selectLabNegativelistHistoryList(LabNegativelistVO vo)
    {
        if (vo.getSemesterId()!=null){
            LabSemester semester = semesterMapper.selectLabSemesterById(vo.getSemesterId());
            vo.setStatTime(DateUtils.dateTime(semester.getSemesterStart()));
            vo.setEndTime(DateUtils.dateTime(semester.getSemesterEnd()));
        }

        return labNegativelistHistoryMapper.selectLabNegativelistHistoryList(vo);
    }

    /**
     * 查询我的负面清单历史记录列表
     *
     * @param vo 负面清单历史记录
     * @return 负面清单历史记录
     */
    @Override
    public List<LabNegativelistHistory> myNegativelistHistoryList(LabNegativelistVO vo)
    {
        if (vo.getSemesterId()!=null){
            LabSemester semester = semesterMapper.selectLabSemesterById(vo.getSemesterId());
            vo.setStatTime(DateUtils.dateTime(semester.getSemesterStart()));
            vo.setEndTime(DateUtils.dateTime(semester.getSemesterEnd()));
        }

        return labNegativelistHistoryMapper.myNegativelistHistoryList(vo);
    }

    /**
     * 新增负面清单历史记录
     *
     * @param labNegativelistHistory 负面清单历史记录
     * @return 结果
     */
    @Override
    public int insertLabNegativelistHistory(LabNegativelistHistory labNegativelistHistory)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labNegativelistHistory);
        return labNegativelistHistoryMapper.insertLabNegativelistHistory(labNegativelistHistory);
    }

    /**
     * 修改负面清单历史记录
     *
     * @param labNegativelistHistory 负面清单历史记录
     * @return 结果
     */
    @Override
    public int updateLabNegativelistHistory(LabNegativelistHistory labNegativelistHistory)
    {
        labNegativelistHistory.setUpdateTime(DateUtils.getNowDate());
        labNegativelistHistory.setUpdateBy(SecurityUtils.getUsername());
        return labNegativelistHistoryMapper.updateLabNegativelistHistory(labNegativelistHistory);
    }

    /**
     * 批量删除负面清单历史记录
     *
     * @param ids 需要删除的负面清单历史记录主键
     * @return 结果
     */
    @Override
    public int deleteLabNegativelistHistoryByIds(Long[] ids)
    {
        return labNegativelistHistoryMapper.deleteLabNegativelistHistoryByIds(ids);
    }

    /**
     * 删除负面清单历史记录信息
     *
     * @param id 负面清单历史记录主键
     * @return 结果
     */
    @Override
    public int deleteLabNegativelistHistoryById(Long id)
    {
        return labNegativelistHistoryMapper.deleteLabNegativelistHistoryById(id);
    }

    @Override
    public List<LabNegativelistHistory> getExpireTimeData() {
        return labNegativelistHistoryMapper.getExpireTimeData();
    }


    @Override
    public List<LabViolationPageListVO> historypageList(String overStatus, String statTime, String endTime) {
        Long userId = tokenService.getLoginUser().getUserid();
        List<LabNegativelistHistory> negativelistHistories = labNegativelistHistoryMapper.historypageList(userId,overStatus,
                statTime,endTime);
        return convertData(negativelistHistories);

    }

    public List<LabViolationPageListVO> convertData(List<LabNegativelistHistory> list){
        List<LabViolationPageListVO> voList = new ArrayList<>();
        for (LabNegativelistHistory h: list) {
            LabViolationPageListVO vo = new LabViolationPageListVO();
            vo.setViolationId(h.getId());
            vo.setCreateTime(h.getCreateTime());
            String now = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分").format(h.getHandleDay());
            vo.setRemake("违规行为导致信用分不足，需在 "+now+" 前，完成所有违规处理");
//            if (h.getWarningStatus()!=null && h.getWarningStatus()!=-1){
//                LabViolationPageListVO.ViolationInfoVO info= new LabViolationPageListVO().new ViolationInfoVO();
//                info.setType(1);
//                info.setContent("违规行为警告，请严格遵守实验室安全管理制度。");
//                info.setStatus(h.getWarningStatus());
//                vo.getViolationList().add(info);
//            }
//            if (h.getInterviewStatus()!=null && h.getInterviewStatus()!=-1){
//                LabViolationPageListVO.ViolationInfoVO info= new LabViolationPageListVO().new ViolationInfoVO();
//                info.setType(2);
//                info.setContent("联系负责老师进行安全教育约谈。");
//                info.setStatus(h.getInterviewStatus());
//                vo.getViolationList().add(info);
//            }
            if (h.getLearnStatus()!=-1){
                LabViolationPageListVO.ViolationInfoVO info= new LabViolationPageListVO().new ViolationInfoVO();
                info.setType(3);
                if ((h.getLearnStatus()!=-1) && (h.getLearnTime() >0)){
                    int hour= (int) (h.getLearnTime()/3600);
                    int m= (int) ((h.getLearnTime()%3600)/60);
                    info.setContent("完成课程学习，时长要求："+hour+"小时"+m+"分钟");
                }
                info.setStatus(h.getLearnStatus());
                vo.getViolationList().add(info);
            }
            if (h.getPracticeStatus()!=-1){
                LabViolationPageListVO.ViolationInfoVO info= new LabViolationPageListVO().new ViolationInfoVO();
                info.setType(4);
                info.setContent("模拟练习，做题正确率要求："+h.getNumberAccuracy()+"%");
                info.setStatus(h.getPracticeStatus());
                vo.getViolationList().add(info);
            }
            if (h.getTestStatus()!=-1){
                LabViolationPageListVO.ViolationInfoVO info= new LabViolationPageListVO().new ViolationInfoVO();
                info.setType(5);
                info.setContent("通过负面清单考试，并且成绩合格.");
                info.setStatus(h.getTestStatus());
                vo.getViolationList().add(info);
            }
            voList.add(vo);
        }
        return voList;
    }


    @Override
    public R updateLabHistory(Long violationId, int status) {
       int row =labNegativelistHistoryMapper.updateLabHistory(violationId,status);
       if (row >0) return R.ok();
       return R.fail();
    }
}
