package com.kcim.service.impl;

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.constants.NumberConstant;
import com.kcim.common.exception.CostException;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.CostOtherPaymentsMapper;
import com.kcim.dao.model.CostOtherPayments;
import com.kcim.dao.model.dto.CostOtherPaymentsEditDto;
import com.kcim.vo.CostOtherPaymentsSaveDto;
import com.kcim.vo.CostOtherPaymentsVO;
import com.kcim.service.CostOtherPaymentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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


@Service("costOtherPaymentsService")
public class CostOtherPaymentsServiceImpl extends ServiceImpl<CostOtherPaymentsMapper, CostOtherPayments> implements CostOtherPaymentsService {


    /**
     * 分压查询全院其他收支设置
     *
     * @param current
     * @param pageSize
     * @param name
     * @param hospId
     * @return
     */
    @Override
    public PageUtils queryList(Integer current, Integer pageSize, String name, Long hospId) {
        Page<CostOtherPayments> costOtherPaymentsPage = new Page<>(current, pageSize);
        Page<CostOtherPayments> pages = this.page(costOtherPaymentsPage, new QueryWrapper<CostOtherPayments>().lambda().eq(CostOtherPayments::getHospId, hospId));
        List<CostOtherPayments> records = pages.getRecords();
        List<CostOtherPaymentsVO> costOtherPaymentsVOList = BeanUtil.convertList(records, CostOtherPaymentsVO.class);
        PageUtils pageUtils = new PageUtils(pages);
        for (CostOtherPaymentsVO vo : costOtherPaymentsVOList) {
            if(vo.getPaymentsType().equals(NumberConstant.ONE)){
                vo.setPaymentsTypeName("收入");
            } else if (vo.getPaymentsType().equals(NumberConstant.TWO)) {
                vo.setPaymentsTypeName("支出");
            }else {
                vo.setPaymentsTypeName("未知");
            }
        }
        pageUtils.setList(costOtherPaymentsVOList);
        return pageUtils;
    }

    /**
     * 根据Id获取对应的数据
     *
     * @param id
     * @return
     */
    @Override
    public CostOtherPaymentsVO getByParamsId(Long id) {
        CostOtherPayments costOtherPayments = this.getById(id);
        if (Objects.isNull(costOtherPayments)){
            return null;
        }
        CostOtherPaymentsVO costOtherPaymentsVO = BeanUtil.convertObj(costOtherPayments, CostOtherPaymentsVO.class);
        return costOtherPaymentsVO;
    }

    /**
     * @param costOtherPaymentsSaveDto
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addOtherPayment(CostOtherPaymentsSaveDto costOtherPaymentsSaveDto) {
        Long hospId = UserContext.getHospId();
        CostOtherPayments otherPayments = this.getOne(new QueryWrapper<CostOtherPayments>().lambda().eq(CostOtherPayments::getHospId, hospId)
        .eq(CostOtherPayments::getPaymentsName,costOtherPaymentsSaveDto.getPaymentsName())
        .eq(CostOtherPayments::getPaymentsType,costOtherPaymentsSaveDto.getPaymentsType()));
        if (Objects.nonNull(otherPayments)){
            throw new CostException(500,"数据已存在");
        }
        CostOtherPayments costOtherPayments = BeanUtil.convertObj(costOtherPaymentsSaveDto, CostOtherPayments.class);
        costOtherPayments.setHospId(hospId);
        costOtherPayments.setCreateTime(System.currentTimeMillis());
        this.save(costOtherPayments);
    }

    /**
     * 修改全院其他收支设置
     *
     * @param costOtherPaymentsEditDto
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateOtherPaymentById(CostOtherPaymentsEditDto costOtherPaymentsEditDto) {
        Long hospId = UserContext.getHospId();
        Long id = costOtherPaymentsEditDto.getId();
        CostOtherPayments otherPayments = this.getOne(new QueryWrapper<CostOtherPayments>().lambda().eq(CostOtherPayments::getHospId, hospId)
                .eq(CostOtherPayments::getId,id));
        if (Objects.isNull(otherPayments)){
            throw new CostException(500,"原始数据不存在");
        }

        this.removeById(id);
        CostOtherPayments otherPaymentResponse = this.getOne(new QueryWrapper<CostOtherPayments>().lambda().eq(CostOtherPayments::getHospId, hospId)
                .eq(CostOtherPayments::getPaymentsName,costOtherPaymentsEditDto.getPaymentsName())
                .eq(CostOtherPayments::getPaymentsType,costOtherPaymentsEditDto.getPaymentsType()));
        if (Objects.nonNull(otherPaymentResponse)){
            throw new CostException(500,"修改后的数据已存在");
        }
        CostOtherPayments costOtherPayments = BeanUtil.convertObj(costOtherPaymentsEditDto, CostOtherPayments.class);
        costOtherPayments.setCreateTime(System.currentTimeMillis());
        costOtherPayments.setId(null);
        costOtherPayments.setHospId(hospId);
        this.save(costOtherPayments);
    }

    /**
     * 查询全院收支设置数据
     *
     * @param hospId
     * @return
     */
    @Override
    public List<CostOtherPaymentsVO> getAll(Long hospId) {
        List<CostOtherPayments> costOtherPaymentsList = this.list(new QueryWrapper<CostOtherPayments>().lambda().eq(CostOtherPayments::getHospId, hospId));
        return BeanUtil.convertList(costOtherPaymentsList, CostOtherPaymentsVO.class);
    }

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