package com.kcim.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostColumn;
import com.kcim.dao.model.CostReportColumn;
import com.kcim.dao.repository.CostColumnRepository;
import com.kcim.dao.repository.CostReportColumnRepository;
import com.kcim.service.CenterService;
import com.kcim.service.CostReportColumnService;
import com.kcim.vo.DictDataVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.kcim.common.constants.Constant.REPORT_COLUMN_TYPE;


@Service("costReportColumnService")
@AllArgsConstructor
@Slf4j
public class CostReportColumnServiceImpl implements CostReportColumnService {

    CostReportColumnRepository repository;

    CostColumnRepository costColumnRepository;

    CenterService centerService;

    /**
     * 查询报表-报表列
     * @param current 当前页
     * @param pageSize 页容量
     * @param reportCode 报表代码
     * @param headText 列标题
     * @return 列表
     */
    @Override
    public Object getReportColumn(Integer current, Integer pageSize, Long reportCode, String headText) {
        Page<CostReportColumn> page = repository.getPage(current, pageSize, reportCode, headText);
        List<CostReportColumn> records = page.getRecords();
        if(CollectionUtils.isEmpty(records)){
           return new PageUtils(new ArrayList<>(), NumberConstant.ZERO,pageSize,current);
        }
        records.sort(Comparator.comparing(CostReportColumn::getSort,Comparator.nullsLast(Integer::compareTo)));
        return new PageUtils(records, Math.toIntExact(page.getTotal()),pageSize,current);
    }

    /**
     * 获取报表列字典
     * @param reportCode 报表代码
     * @param headText 列标题
     * @param columnType 列类型
     * @return 报表列字典
     */
    @Override
    public Object getReportCostColumn(Long reportCode, String headText, String columnType) {
        List<CostColumn> list = costColumnRepository.getList(headText, columnType);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<CostReportColumn> reportColumns = repository.getList(reportCode);
        if(!CollectionUtils.isEmpty(reportColumns)){
            Map<Long,CostReportColumn> map = reportColumns.stream().collect(Collectors.toMap(CostReportColumn::getColumnCode, reportColumn -> reportColumn, (a, b) -> b));
            List<CostColumn> removeColumn = new ArrayList<>();
            for (CostColumn costColumn : list) {
                CostReportColumn costReportColumn = map.get(costColumn.getCode());
                if(Objects.nonNull(costReportColumn)){
                    removeColumn.add(costColumn);
                }
            }
            if(!CollectionUtils.isEmpty(removeColumn)){
                list.removeAll(removeColumn);
            }
        }
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        DictDataVo centerDict = centerService.getDict(REPORT_COLUMN_TYPE);
        Map<String,String> map = centerDict.getDataVoList().stream().collect(Collectors.toMap(DictDataVo::getCode, DictDataVo::getName, (a, b) -> b));
        for(CostColumn column:list){
            if(!CollectionUtils.isEmpty(map)){
                column.setTypeName(map.get(column.getType()));
            }
        }
        return list;
    }

    /**
     * @param reportColumn
     */
    @Override
    public void editReportColumn(List<CostReportColumn> reportColumn) {
        if(!CollectionUtils.isEmpty(reportColumn)){
            for(CostReportColumn column:reportColumn){
                column.setHospId(UserContext.getCurrentLoginHospId());
                column.setUpdateTime(new Date());
                column.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            repository.updateBatchById(reportColumn);
        }
    }

    /**
     * @param reportColumn
     */
    @Override
    public void addReportColumn(List<CostReportColumn> reportColumn) {
        if(!CollectionUtils.isEmpty(reportColumn)){
//            Long reportCode = reportColumn.get(0).getReportCode();
//           repository.deleteReportColumnByReportCode(reportCode);
            for (CostReportColumn column : reportColumn) {
                column.setHospId(UserContext.getHospId());
                column.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
                column.setCreateTime(new Date());
                column.setHide(NumberConstant.ZERO);
                column.setFreeze(NumberConstant.ZERO);
                column.setPrimaryKey(NumberConstant.ZERO);
                column.setSortStatus(NumberConstant.ZERO);
            }
            repository.saveBatch(reportColumn);
        }

    }

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

    /**
     *
     * @param id
     */
    @Override
    public void batchDeleteReportColumn(List<Integer> id) {
        repository.batchDeleteReportColumn(id);
    }

    @Override
    public void editReportColumnStatus(Integer id, Integer type, Integer status) {
        CostReportColumn column = repository.getById(id);
        if(type.equals(NumberConstant.ONE)){
            //是否主键 1是 0否
            column.setPrimaryKey(status);
        } else if (type.equals(NumberConstant.TWO)) {
            //是否隐藏 1是 0否
            column.setHide(status);
        } else if (type.equals(NumberConstant.THREE)) {
            //是否冻结 1是 0否
            column.setFreeze(status);
        } else if (type.equals(NumberConstant.FOUR)) {
            //是否排序 1是 0否
            column.setSortStatus(status);
        }
        repository.updateById(column);

    }
}