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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.product.dto.SyncNoticeReadDTO;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.SyncNotice;
import com.czg.account.service.SyncNoticeService;
import com.czg.service.account.mapper.SyncNoticeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author zs
 * @since 2025-04-07
 */
@DubboService
public class SyncNoticeServiceImpl extends ServiceImpl<SyncNoticeMapper, SyncNotice> implements SyncNoticeService {

    @Override
    public void addNotice(Long shopId, Long sysUserId, String name, Long id, Integer type) {
        SyncNotice syncNotice = new SyncNotice();
        syncNotice.setShopId(shopId);
        syncNotice.setSysUserId(sysUserId);
        String content = switch (type) {
            case 0 -> "新增商品";
            case 1 -> "修改商品";
            case 2 -> "新增耗材";
            case 3 -> "修改耗材";
            default -> "";
        };

        String json = switch (type) {
            case 0, 1, 2, 3 -> StrUtil.format("{\"id\":{},\"name\":\"{}\"}", id, name);
            default -> "";
        };

        content = StrUtil.format("{}: {}, 请及时确认;", content, name);
        syncNotice.setContent(content);
        syncNotice.setExtraJson(json);
        save(syncNotice);
    }

    @Override
    public void addNotice(Long shopId, Long sysUserId, String title, String content, String extraJson) {
        SyncNotice syncNotice = new SyncNotice();
        syncNotice.setShopId(shopId);
        syncNotice.setSysUserId(sysUserId);
        syncNotice.setTitle(title);
        syncNotice.setContent(content);
//        syncNotice.setType(type);
//        syncNotice.setExtraJson(extraJson);
        save(syncNotice);
    }


    @Override
    public Page<SyncNotice> pageInfo(Long shopId, String name, String startTime, String endTime, String title, Integer isRead) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(SyncNotice::getShopId, shopId);
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(SyncNotice::getContent, name);
        }

        if (StrUtil.isNotBlank(startTime)) {
            queryWrapper.ge(SyncNotice::getCreateTime, startTime);
        }

        if (StrUtil.isNotBlank(endTime)) {
            queryWrapper.le(SyncNotice::getCreateTime, endTime);
        }
        queryWrapper.eq(SyncNotice::getTitle, title);
        queryWrapper.eq(SyncNotice::getIsRead, isRead);
        queryWrapper.orderBy(SyncNotice::getCreateTime, false);
        return page(PageUtil.buildPage(), queryWrapper);
    }

    @Override
    public Boolean read(Long shopId, SyncNoticeReadDTO syncNoticeReadDTO) {
        if (syncNoticeReadDTO.getNoticeIdList() == null || syncNoticeReadDTO.getNoticeIdList().isEmpty()) {
            return updateChain().eq(SyncNotice::getShopId, shopId).set(SyncNotice::getIsRead, 1).set(SyncNotice::getReadTime, DateUtil.date().toLocalDateTime()).update();
        }
        List<SyncNotice> listed = list(new QueryWrapper().eq(SyncNotice::getShopId, shopId).in(SyncNotice::getId, syncNoticeReadDTO.getNoticeIdList()));
        listed.forEach(item -> {
            if (item.getIsRead() == 0) {
                item.setIsRead(1);
                item.setReadTime(DateUtil.date().toLocalDateTime());
            }
        });
        return updateBatch(listed);
    }
}
