package com.kcim.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.exception.CostException;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.DateUtils;
import com.kcim.common.util.PageUtils;
import com.kcim.common.enums.DateStyleEnum;
import com.kcim.dao.mapper.CostOtherPaymentsDataMapper;
import com.kcim.dao.model.CostOtherPaymentsData;
import com.kcim.dao.model.dto.CostOtherPaymentsDataEditDto;
import com.kcim.dao.model.dto.CostOtherPaymentsDataSaveDto;
import com.kcim.vo.CostOtherPaymentsDataVO;
import com.kcim.service.CostOtherPaymentsDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service("costOtherPaymentsDataService")
public class CostOtherPaymentsDataServiceImpl extends ServiceImpl<CostOtherPaymentsDataMapper, CostOtherPaymentsData> implements CostOtherPaymentsDataService {


    /**
     * 分页查询
     *
     * @param current  第几页
     * @param pageSize 每页大小
     * @param dateTime 时间
     * @param hospId   医院Id
     * @return
     */
    @Override
    public PageUtils queryList(Integer current, Integer pageSize, String dateTime, Long hospId) {
        // 先检验当前年月是否存在数据
        int year = 0;
        int month = 0;
        Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
        if (StrUtil.isNotBlank(dateTime)) {
            year = DateUtil.year(date);
            month = DateUtil.month(date) + 1;
        }
        Page<CostOtherPaymentsData> paymentsDataPage = new Page<>(current, pageSize);
        Page<CostOtherPaymentsData> pages = this.page(paymentsDataPage, new QueryWrapper<CostOtherPaymentsData>().lambda()
                .eq(CostOtherPaymentsData::getHospId, hospId)
                .eq(StrUtil.isNotBlank(dateTime), CostOtherPaymentsData::getDateYear, year)
                .eq(StrUtil.isNotBlank(dateTime), CostOtherPaymentsData::getDateMonth, month)
                .orderByDesc(CostOtherPaymentsData::getCreateTime));
        List<CostOtherPaymentsData> records = pages.getRecords();
        List<CostOtherPaymentsDataVO> costOtherPaymentsDataVOList = BeanUtil.convertList(records, CostOtherPaymentsDataVO.class);
        PageUtils pageUtils = new PageUtils(pages);
        pageUtils.setList(costOtherPaymentsDataVOList);
        return pageUtils;
    }

    /**
     * 保存全院其他收支数据
     *
     * @param costOtherPaymentsDataSaveDto 全院其他收支
     * @param hospId                       医院的Id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId) {
        String dateTime = costOtherPaymentsDataSaveDto.getDateTime();
        // 先检验当前年月是否存在数据
        checkOtherPaymentData(costOtherPaymentsDataSaveDto, hospId, dateTime);
        CostOtherPaymentsData costOtherPaymentsData = BeanUtil.convertObj(costOtherPaymentsDataSaveDto, CostOtherPaymentsData.class);
        int year = 0;
        int month = 0;
        Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
        if (StrUtil.isNotBlank(dateTime)) {
            year = DateUtil.year(date);
            month = DateUtil.month(date) + 1;
        }
        costOtherPaymentsData.setHospId(hospId);
        costOtherPaymentsData.setCreateTime(System.currentTimeMillis());
        costOtherPaymentsData.setDateYear(year);
        costOtherPaymentsData.setDateMonth(month);
        this.save(costOtherPaymentsData);
    }

    // 先检验当前年月是否存在数据
    private void checkOtherPaymentData(CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto, Long hospId, String dateTime) {
        int year = 0;
        int month = 0;
        Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
        if (StrUtil.isNotBlank(dateTime)) {
            year = DateUtil.year(date);
            month = DateUtil.month(date) + 1;
        }
        Integer paymentsType = costOtherPaymentsDataSaveDto.getPaymentsType();
        String paymentsName = costOtherPaymentsDataSaveDto.getPaymentsName();
        CostOtherPaymentsData paymentsData = this.getOne(new QueryWrapper<CostOtherPaymentsData>().lambda()
                .eq(CostOtherPaymentsData::getHospId, hospId)
                .eq(CostOtherPaymentsData::getDateYear, year).eq(CostOtherPaymentsData::getDateMonth, month)
                .eq(CostOtherPaymentsData::getPaymentsType, paymentsType).eq(CostOtherPaymentsData::getPaymentsName, paymentsName));
        if (Objects.nonNull(paymentsData)) {
            throw new CostException(500, year + "年" + month + "月" + paymentsName + "已存在");
        }
    }

    /**
     * 修改全院其他收支数据
     *
     * @param costOtherPaymentsDataEditDto
     * @param hospId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateOtherPaymentData(CostOtherPaymentsDataEditDto costOtherPaymentsDataEditDto, Long hospId) {
        String dateTime = costOtherPaymentsDataEditDto.getDateTime();
        int year = 0;
        int month = 0;
        if (StrUtil.isNotBlank(dateTime)) {
            Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
            year = DateUtil.year(date);
            month = DateUtil.month(date) + 1;
        }
        Long id = costOtherPaymentsDataEditDto.getId();
        CostOtherPaymentsData otherPaymentsData = this.getById(id);
        if (Objects.isNull(otherPaymentsData)) {
            throw new CostException(500, "全院其他收支数据不存在");
        }
        this.removeById(id);
        CostOtherPaymentsDataSaveDto costOtherPaymentsDataSaveDto = BeanUtil.convertObj(costOtherPaymentsDataEditDto, CostOtherPaymentsDataSaveDto.class);
        // 检验数据
        checkOtherPaymentData(costOtherPaymentsDataSaveDto, hospId, dateTime);
        // 实现数据添加
        CostOtherPaymentsData costOtherPaymentsData = BeanUtil.convertObj(costOtherPaymentsDataSaveDto, CostOtherPaymentsData.class);
        costOtherPaymentsData.setCreateTime(System.currentTimeMillis());
        costOtherPaymentsData.setHospId(hospId);
        costOtherPaymentsData.setDateYear(year);
        costOtherPaymentsData.setDateMonth(month);
        this.save(costOtherPaymentsData);
    }


    /**
     * 某个月全月其他收支
     *
     * @param year   年
     * @param month  月
     * @param hospId 医院id
     * @return
     */
    @Override
    public List<CostOtherPaymentsData> getByMonth(int year, int month, Long hospId) {
        return this.list(
                new LambdaQueryWrapper<CostOtherPaymentsData>()
                        .eq(CostOtherPaymentsData::getDateYear, year)
                        .eq(CostOtherPaymentsData::getDateMonth, month)
                        .eq(CostOtherPaymentsData::getHospId, hospId)
        );
    }

    /**
     * 批量删除全院其他收支设置数据
     *
     * @param idList 全院其他收支设置数据的集合
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteByIds(List<Long> idList) {
        this.removeByIds(idList);
    }
}