
package com.tmerclub.cloud.delivery.service.impl;

import com.tmerclub.cloud.common.cache.constant.DeliveryCacheNames;
import com.tmerclub.cloud.common.constant.SysTypeEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.delivery.dto.PrinterDTO;
import com.tmerclub.cloud.delivery.mapper.PrinterMapper;
import com.tmerclub.cloud.delivery.model.Printer;
import com.tmerclub.cloud.delivery.service.PrinterService;
import com.tmerclub.cloud.delivery.vo.PrinterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

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

/**
 * 打印机
 *
 * @author TRACK
 * @date 2023-08-23 14:48:23
 */
@Service
public class PrinterServiceImpl implements PrinterService {

    @Autowired
    private PrinterMapper printerMapper;

    @Override
    public PageVO<Printer> page(PageDTO pageDTO, PrinterDTO printerDTO) {
        return PageUtil.doPage(pageDTO, () -> printerMapper.list(printerDTO));
    }

    @Override
    public Printer getByPrinterId(Long printerId) {
        // TODO : 本地暂时注释
        // Integer sysType = AuthUserContext.get().getSysType();
        Printer printer = printerMapper.getByPrinterId(printerId);
        if (Objects.isNull(printer)) {
            throw new LuckException("打印机信息为空");
        }
//        if (Objects.equals(sysType, SysTypeEnum.SUPPLIER.value())) {
//            if (!Objects.equals(printer.getSupplierId(), AuthUserContext.get().getTenantId())) {
//                throw new LuckException("暂无权限查看该配置");
//            }
//        } else {
//            if (!Objects.equals(printer.getShopId(), AuthUserContext.get().getTenantId())) {
//                throw new LuckException("暂无权限查看该配置");
//            }
//        }
        return printer;
    }

    @Override
    public void save(Printer printer) {
        checkInfo(printer);
        printerMapper.save(printer);
    }

    private void checkInfo(Printer printer) {
        int count = printerMapper.countByName(printer.getShopId(), printer.getSupplierId(), printer.getPrinterName(), printer.getPrinterId());
        if (count > 0) {
            throw new LuckException("设备名称已存在");
        }
    }

    @Override
    public void update(Printer printer) {
        checkInfo(printer);
        printerMapper.update(printer);
    }

    @Override
    public void deleteById(Long printerId) {
        printerMapper.deleteById(printerId);
    }

    @Override
    public void setDefault(Long printerId) {
        Printer printer = getByPrinterId(printerId);
        if (Objects.isNull(printer)) {
            throw new LuckException("设备不存在");
        }
        if (Objects.equals(printer.getIsDefault(), 0)) {
            // 设为默认
            printerMapper.cancelDefault(printer.getShopId(), printer.getSupplierId());
        }
        printerMapper.setDefault(printerId);
    }

    @Override
    @Cacheable(cacheNames = DeliveryCacheNames.PRINTER_LIST_KEY, key = "#shopId + ':' + #sysType")
    public List<PrinterVO> listByShopId(Long shopId, Integer sysType) {
        return printerMapper.listByShopId(shopId, sysType);
    }

    @Override
    @CacheEvict(cacheNames = DeliveryCacheNames.PRINTER_LIST_KEY, key = "#shopId + ':' + #sysType")
    public void removeCache(Long shopId, Integer sysType) {}
}
