package com.kcim.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.PageUtils;
import com.kcim.common.constants.NumberConstant;
import com.kcim.dao.mapper.DepartmentProfitFileMapper;
import com.kcim.dao.model.DepartmentProfitFile;
import com.kcim.dao.model.FileRecord;
import com.kcim.vo.FileRecordDepartmentVO;
import com.kcim.service.DepartmentProfitFileService;
import com.kcim.service.FileRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("departmentProfitFileService")
public class DepartmentProfitFileServiceImpl extends ServiceImpl<DepartmentProfitFileMapper, DepartmentProfitFile> implements DepartmentProfitFileService {

    private final FileRecordService fileRecordService;

    public DepartmentProfitFileServiceImpl(FileRecordService fileRecordService) {
        this.fileRecordService = fileRecordService;
    }

    /**
     * 分页查询进行报表记录
     *
     * @param current    当前页
     * @param pageSize   每一页大小
     * @param date       时间xxx-xx-xx
     * @param reportName 报表名称
     * @param hospId     医院Id
     * @return
     */
    @Override
    public PageUtils queryList(Integer current, Integer pageSize, String date, String reportName, Long hospId) {
        DateTime dateTime;
        int dateYear = 0;
        int month=0;
        if (StrUtil.isNotBlank(date)){
            dateTime = DateUtil.parse(date);
            dateYear = DateUtil.year(dateTime);
            month=DateUtil.month(dateTime)+1;
        }
        Page<FileRecord> profitFilePage = new Page<>(current, pageSize);
        Page<FileRecord> pages = fileRecordService.page(profitFilePage, new QueryWrapper<FileRecord>()
                .lambda()
                .eq(FileRecord::getHospId, hospId).eq(FileRecord::getFileSource, NumberConstant.ZERO)
                .eq(StrUtil.isNotBlank(date), FileRecord::getDateYear, dateYear)
                .eq(StrUtil.isNotBlank(date), FileRecord::getDateMonth, month)
                .orderByDesc(FileRecord::getCreateTime));
        List<FileRecord> records = pages.getRecords();
        List<FileRecordDepartmentVO> departmentProfitFileVOList = BeanUtil.convertList(records, FileRecordDepartmentVO.class);
        departmentProfitFileVOList.forEach(i->{
            i.setUpdateTime(DateUtil.format(DateUtil.date(i.getCreateTime()),"yyyy-MM-dd HH:mm:ss"));
        });
        PageUtils pageUtils = new PageUtils(pages);
        pageUtils.setList(departmentProfitFileVOList);
        return pageUtils;
    }

    /**
     * 添加科室损益记录
     *
     * @param date       时间
     * @param reportType 报表类型
     * @param hospId     医院ID
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addDepartmentProfitFile(String date, Integer reportType, Long hospId) {
        DepartmentProfitFile departmentProfitFile = new DepartmentProfitFile();
        DateTime dateTime = DateUtil.parse(date);
        int year = DateUtil.year(dateTime);
        int month = DateUtil.month(dateTime)+1;
        departmentProfitFile.setHospId(hospId);
        departmentProfitFile.setYear(year);
        departmentProfitFile.setMonth(month);
        departmentProfitFile.setReportType(reportType);
        String reportName=reportType==1?year+"年"+month+"月-损益表.xlsx":year+"年"+month+"月-其他表.xlsx";
        departmentProfitFile.setReportName(reportName);
        departmentProfitFile.setCreateTime(System.currentTimeMillis());
        this.save(departmentProfitFile);
    }

    /**
     * 批量删除科室损益数据
     *
     * @param idList 科室损益的Id的集合
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteByIds(List<Long> idList) {
        this.removeByIds(idList);
    }

}