package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.sales.pojo.dto.customer.BulletinDTO;
import com.storlead.sales.pojo.dto.customer.BulletinSearchDTO;
import com.storlead.sales.pojo.entity.customer.BulletinReadEntity;
import com.storlead.sales.pojo.vo.customer.BulletinDetailVO;
import com.storlead.sales.service.customer.BulletinService;
import com.storlead.sales.service.impl.customer.BulletinReadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 公告主表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-10-25
 */
@RestController
@RequestMapping("/bulletin")
@Api(tags = "销售: 公告相关接口")
public class BulletinController {

    @Resource
    private BulletinService bulletinService;
    @Resource
    private BulletinReadServiceImpl bulletinReadServiceImpl;

    @PostMapping(value = "/list")
    @ApiOperation(value = "公告相关接口-公告列表", notes = "公告相关接口-公告列表")
    public Result<?> list(@ApiParam(value = "公告列表请求类") BulletinSearchDTO bulletinSearchDTO) {
        return bulletinService.listVO(bulletinSearchDTO);
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @PostMapping(value = "/get-by-id")
    @ApiOperation(value = "公告-通过id查看详情", notes = "公告-通过id查看详情")
    public Result<BulletinDetailVO> getById(@RequestParam(name="id",required=false) Long id, Long userId) {
        BulletinDetailVO bulletinDetailVO = bulletinService.getBulletinDetailVOById(id,userId);
        SysBaseField.orgFileds(bulletinDetailVO);
        return Result.result(bulletinDetailVO);
    }


    @GetMapping(value = "/unread-count")
    @ApiOperation(value = "公告相关接口-未读公告统计", notes = "公告相关接口-未读公告统计")
    public Result<?> unreadCount() {
        return bulletinService.unreadCount();
    }

    @PostMapping(value = "/is-read")
    @ApiOperation(value = "公告相关接口-一键已读", notes = "公告相关接口-一键已读")
    public Result<?> isRead() {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        LambdaUpdateWrapper<BulletinReadEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BulletinReadEntity::getReadUserId,currentUserId);
        wrapper.eq(BulletinReadEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.set(BulletinReadEntity::getHasRead,true);
        bulletinReadServiceImpl.update(wrapper);
        return Result.ok();
    }

    /**
     * 添加
     *
     * @param bulletinDTO
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "公告相关接口-添加", notes = "公告相关接口-添加目标")
    public Result<?> add(@RequestBody @ApiParam(value = "bulletinDTO") BulletinDTO bulletinDTO) {
        return  bulletinService.add(bulletinDTO);
    }

    /**
     * 编辑
     *
     * @param bulletinDTO
     * @return
     */
    @PutMapping(value = "/edit")
    @ApiOperation(value = "公告相关接口-编辑公告", notes = "公告相关接口-编辑公告")
    public Result<?> edit(@RequestBody BulletinDTO bulletinDTO) {
        return  bulletinService.edit(bulletinDTO);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "公告相关接口-删除", notes = "公告相关接口-删除")
    public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
        return bulletinService.delete(id);
    }
}
