package com.kcim.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostReportRedirect;
import com.kcim.dao.repository.CostReportRedirectRepository;
import com.kcim.service.CostReportRedirectService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("costReportRedirectService")
@Slf4j
@AllArgsConstructor
public class CostReportRedirectServiceImpl implements CostReportRedirectService {

    CostReportRedirectRepository repository;

    /**
     * @param current
     * @param pageSize
     * @param reportName
     * @return
     */
    @Override
    public Object getRedirect(Integer current, Integer pageSize, String reportName) {
        Page<CostReportRedirect> page = repository.getPage(current, pageSize, reportName);
        List<CostReportRedirect> records = page.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageUtils(new ArrayList<>(), NumberConstant.ZERO,pageSize,current);
        }
        return new PageUtils(records, Math.toIntExact(page.getTotal()),pageSize,current);
    }

    /**
     * @param reportRedirect
     */
    @Override
    public void addRedirect(CostReportRedirect reportRedirect) {
        reportRedirect.setHospId(UserContext.getCurrentLoginHospId());
        reportRedirect.setCreateTime(new Date());
        reportRedirect.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        reportRedirect.setCode(repository.getCode());
        repository.save(reportRedirect);
    }

    /**
     * @param reportRedirect
     */
    @Override
    public void editRedirect(CostReportRedirect reportRedirect) {
        Integer id = reportRedirect.getId();
        CostReportRedirect reportRedirect1 = repository.getById(id);
        BeanUtil.convertObj(reportRedirect,reportRedirect1);
        reportRedirect1.setUpdateTime(new Date());
        reportRedirect1.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        repository.updateById(reportRedirect);
    }

    /**
     * @param id
     */
    @Override
    public void deleteRedirect(Integer id) {
        repository.deleteRedirect(id);
    }
}