package com.czg.service.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.PrintMachine;
import com.czg.account.service.PrintMachineService;
import com.czg.exception.CzgException;
import com.czg.service.account.mapper.PrintMachineMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 打印机设备 服务层实现。
 *
 * @author zs
 * @since 2025-02-20
 */
@DubboService
public class PrintMachineServiceImpl extends ServiceImpl<PrintMachineMapper, PrintMachine> implements PrintMachineService {

    @Override
    public boolean add(Long shopId, PrintMachine dto) {
        dto.setShopId(shopId);
        if ("1".equals(dto.getClassifyPrint())) {
            if (StrUtil.isBlank(dto.getCategoryIds())) {
                throw new CzgException("分类打印选择部分打印时，必须勾选需要部分打印的分类");
            }
        } else {
            dto.setCategoryIds(null);
        }
        return save(dto);
    }

    @Override
    public Boolean edit(Long shopId, PrintMachine printerEditDTO) {
        printerEditDTO.setShopId(shopId);
        if ("1".equals(printerEditDTO.getClassifyPrint())) {
            if (StrUtil.isBlank(printerEditDTO.getCategoryIds())) {
                throw new CzgException("分类打印选择部分打印时，必须勾选需要部分打印的分类");
            }
        } else {
            printerEditDTO.setCategoryIds(null);
        }
        PrintMachine printMachine = getOne(new QueryWrapper().eq(PrintMachine::getShopId, shopId).eq(PrintMachine::getId, printerEditDTO.getId()));
        if (printMachine == null) {
            throw new CzgException("打印机不存在");
        }

        BeanUtil.copyProperties(printerEditDTO, printMachine);
        return updateById(printMachine);
    }
}
