package com.ho.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ho.business.common.MinioNewUtil;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.RepairFile;
import com.ho.business.entity.StationNotepad;
import com.ho.business.entity.StationRepair;
import com.ho.business.mapper.StationNoteAndRepairMapper;
import com.ho.business.service.StationNoteAndRepairService;
import com.ho.business.vo.req.StationNotepadReq;
import com.ho.business.vo.req.StationRepairReqVo;
import com.ho.business.vo.resp.StationRepairRespVo;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StationNoteAndRepairServiceImpl implements StationNoteAndRepairService {

    @Autowired
    StationNoteAndRepairMapper stationNoteAndRepairMapper;

    @Autowired
    MinioNewUtil minioUtil;

    @Override
    public void addStationNote(StationNotepad stationNotepad) {
        stationNotepad.setCreateTime(DateUtil.format(new Date(), CommonConstant.DATE));
        stationNoteAndRepairMapper.addStationNote(stationNotepad);
    }

    @Override
    public void deleteStationNote(Integer id) {
        stationNoteAndRepairMapper.deleteStationNote(id);
    }

    @Override
    public void updateStationNote(StationNotepad stationNotepad) {
        stationNotepad.setUpdateTime(DateUtil.format(new Date(), CommonConstant.DATE));
        stationNoteAndRepairMapper.updateStationNote(stationNotepad);

    }

    @Override
    public List<StationNotepad> selectStationNote(StationNotepadReq stationNotepadReq) {
        List<StationNotepad>  result = stationNoteAndRepairMapper.selectStationNote(stationNotepadReq);
        return result;
    }

    @Override
    @Transactional
    public void addStationRepair(StationRepairReqVo stationRepairReqVo) {
        //首先新增维修记录相关数据
        StationRepair stationRepair = new StationRepair();
        BeanUtils.copyProperties(stationRepairReqVo,stationRepair);
        stationRepair.setCreateTime(DateUtil.format(new Date(),CommonConstant.DATE));
        stationNoteAndRepairMapper.addStationRepair(stationRepair);
        List<RepairFile> repairFileList = stationRepairReqVo.getRepairFileList();
        if(repairFileList!= null && repairFileList.size()!= 0){
            stationNoteAndRepairMapper.updateRepairFile(repairFileList,stationRepair.getId());
        }
    }

    @Override
    @Transactional
    public void deleteStationRepair(Integer id,String bucket) {
        //删除维修记录
        stationNoteAndRepairMapper.deleteStationRepair(id);
        //获取当前维修记录对应的附件记录
        List<RepairFile> repairFileList = new ArrayList<>();
        repairFileList =  stationNoteAndRepairMapper.selectRepairFile(id);
        if(repairFileList.size()>0){
//            for(RepairFile repairFile:repairFileList){
//                boolean flag = minioUtil.deleteFile(bucket,  repairFile.getFileUrl());
//                if (!flag) {
//                    throw new BusinessException(BaseResponseCode.DELETE_ERROR);
//                }
//            }
            //删除维修记录对应的附件记录
            stationNoteAndRepairMapper.deleteRepairInfo(id);
        }

    }

    @Override
    @Transactional
    public void updateStationRepair(StationRepairReqVo stationRepairReqVo) {
        //更新维修记录
        stationNoteAndRepairMapper.updateStationRepair(stationRepairReqVo);
        List<RepairFile> repairFileList = stationRepairReqVo.getRepairFileList();
        if(repairFileList!= null && repairFileList.size()!= 0){
            //先将之前的文件信息关联关系赋空
            stationNoteAndRepairMapper.updateFileRepairNull(stationRepairReqVo.getId());
            //保存新的关联关系
            stationNoteAndRepairMapper.updateRepairFile(repairFileList,stationRepairReqVo.getId());
        }else{
            //删除原有文件关联关系
            stationNoteAndRepairMapper.updateFileRepairNull(stationRepairReqVo.getId());
        }
    }

    @Override
    public List<StationRepairRespVo> selectStationRepair(StationRepairReqVo stationRepairReqVo) {
        //首先按条件查询维修记录
        List<StationRepair> stationRepairs = stationNoteAndRepairMapper.selectStationRepair(stationRepairReqVo);
        //根据维修记录查询对应附件记录
        List<StationRepairRespVo> result =  new ArrayList<>();
        result = stationRepairs.stream().map(s ->{
            StationRepairRespVo stationRepairRespVo = new StationRepairRespVo();
            BeanUtils.copyProperties(s,stationRepairRespVo);
            List<RepairFile> repairFileList = new ArrayList<>();
            repairFileList =  stationNoteAndRepairMapper.selectRepairFile(s.getId());
            stationRepairRespVo.setRepairFileList(repairFileList);
            Boolean isHaveFile = repairFileList.size()>0;
            stationRepairRespVo.setIsHaveFile(isHaveFile);
            return stationRepairRespVo;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public RepairFile uploadRepairFile(MultipartFile file, String bucket) {
        //判断该集团的bucket是否存在,不存在就创建
        boolean flag = minioUtil.bucketExists(bucket);
        if (!flag) {
            minioUtil.createBucketName(bucket);
        }
        Calendar c = Calendar.getInstance(); // 创建Calendar对象
        int year = c.get(Calendar.YEAR); // 获取年
        int month = c.get(Calendar.MONTH) + 1; // 获取月份 0表示1月份
        int day = c.get(Calendar.DAY_OF_MONTH); // 获取日期
        int hour = c.get(Calendar.HOUR_OF_DAY); // 获取小时
        int minute = c.get(Calendar.MINUTE); // 获取分钟
        int second = c.get(Calendar.SECOND); // 获取秒
        String str = year + "/" + month + "/" + day + "/" + hour + "/" + minute + "/" + second;
        String filePathName = DeviceTypeConstant.STATION + "/" + str + "/" + file.getOriginalFilename();
        String url = minioUtil.minioUpload(file, filePathName, bucket);
        RepairFile repairFile = new RepairFile();
        repairFile.setFileName(file.getOriginalFilename());
        repairFile.setUrl(url);
        stationNoteAndRepairMapper.addRepairFile(repairFile);
        return repairFile;
    }
}
