package com.czg.controller.admin;

import com.czg.account.entity.SyncNotice;
import com.czg.account.service.SyncNoticeService;
import com.czg.product.dto.SyncNoticeReadDTO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 通知中心-同步消息
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/syncNotice")
public class SyncNoticeController {
    @Resource
    private SyncNoticeService syncNoticeService;

    /**
     * 通知消息列表
     * @param name 名称
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @param title  数据同步/数据变动/库存预警
     * @param isRead 0-未读 1-已读
     * @return 分页数据
     */
    @GetMapping
    public CzgResult<Page<SyncNotice>> page(@RequestParam(required = false) String name, @RequestParam(required = false) String startTime,
                                            @RequestParam(required = false) String endTime, @RequestParam(required = false) String title,
                                            @RequestParam(required = false) Integer isRead) {
        return CzgResult.success(syncNoticeService.pageInfo(StpKit.USER.getShopId(), name, startTime, endTime, title, isRead));
    }

    /**
     * 详情
     * @param id id
     * @return 详细信息
     */
    @GetMapping("/detail")
    public CzgResult<SyncNotice> detail(@RequestParam Long id) {
        return CzgResult.success(syncNoticeService.getOne(new QueryWrapper().eq(SyncNotice::getShopId, StpKit.USER.getShopId()).eq(SyncNotice::getId, id)));
    }


    /**
     * 已读消息, 不传递已读所有
     * @return 是否成功
     */
    @PutMapping("/read")
    public CzgResult<Boolean> read(@RequestBody @Validated SyncNoticeReadDTO syncNoticeReadDTO) {
        return CzgResult.success(syncNoticeService.read(StpKit.USER.getShopId(), syncNoticeReadDTO));
    }

    /**
     * 清空已读
     * @return 是否成功
     */
    @DeleteMapping("/clear")
    public CzgResult<Boolean> clear() {
        return CzgResult.success(syncNoticeService.remove(new QueryWrapper().eq(SyncNotice::getShopId, StpKit.USER.getShopId()).eq(SyncNotice::getIsRead, 1)));
    }

    /**
     * 删除
     * @param id id
     * @return 是否成功
     */
    @DeleteMapping
    public CzgResult<Boolean> delete(@RequestParam Long id) {
        return CzgResult.success(syncNoticeService.remove(new QueryWrapper().eq(SyncNotice::getShopId, StpKit.USER.getShopId()).eq(SyncNotice::getId, id)));
    }


    /**
     * 消息统计
     * @return 消息记录数
     */
    @GetMapping("/count")
    public CzgResult<Long> count(@RequestParam(required = false) Integer isRead) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(SyncNotice::getShopId, StpKit.USER.getShopId());
        queryWrapper.eq(SyncNotice::getIsRead, isRead);
        return CzgResult.success(syncNoticeService.count(queryWrapper));
    }

}
