package shop.alien.store.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeNotice;
import shop.alien.entity.store.vo.LifeNoticeVo;
import shop.alien.mapper.LifeNoticeMapper;
import shop.alien.store.service.LifeNoticeService;
import shop.alien.util.type.LifeNoticeUtil;

import java.util.List;

@Api(tags = {"一期-公告"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("notice")
@RequiredArgsConstructor
public class LifeNoticeController {

    private final LifeNoticeMapper lifeNoticeMapper;

    private final LifeNoticeUtil lifeNoticeUtil;

    private final LifeNoticeService lifeNoticeService;

    @GetMapping("/getNoticeByPhoneId")
    @ApiOperation("通知列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "noticeType", value = "0-系统通知和订单提醒之外的类型 1-系统通知 2-订单提醒", dataType = "Integer", paramType = "query")})
    public R<IPage<LifeNoticeVo>> getNoticeByPhoneId(@RequestParam int pageNum,
                                                    @RequestParam int pageSize,
                                                    @RequestParam String receiverId,
                                                    @RequestParam(defaultValue = "0") int noticeType) {
        log.info("LifeNoticeController.getNoticeByPhoneId?pageNum={}, pageSize={}, receiverId={}, noticeType={}",pageNum, pageSize, receiverId, noticeType);
        return lifeNoticeService.getNoticeList(pageNum, pageSize, receiverId, noticeType);
    }

    @GetMapping("/getSystemAndOrderNoticeSum")
    @ApiOperation("通知数量")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    public R<JSONObject> getSystemAndOrderNoticeSum(@RequestParam String receiverId) {
        log.info("LifeNoticeController.getSystemAndOrderNoticeSum?receiverId={}", receiverId);
        return R.data(lifeNoticeService.getSystemAndOrderNoticeSum(receiverId));
    }

    @GetMapping("/countUnreadByType")
    @ApiOperation("按通知类型和接收人查询未读消息数量")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "receiverId", value = "接收人ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "noticeType", value = "通知类型 0-系统通知和订单提醒之外 1-系统通知 2-订单提醒", dataType = "Integer", paramType = "query", required = true)
    })
    public R<Long> countUnreadByReceiverIdAndNoticeType(@RequestParam String receiverId,
                                                        @RequestParam Integer noticeType) {
        log.info("LifeNoticeController.countUnreadByReceiverIdAndNoticeType?receiverId={}, noticeType={}", receiverId, noticeType);
        return R.data(lifeNoticeService.countUnreadByReceiverIdAndNoticeType(receiverId, noticeType));
    }

    @GetMapping("/deleteNoticeById")
    @ApiOperation("删除通知")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query")})
    public R<Boolean> deleteNoticeById(Integer id) {
        log.info("LifeNoticeController.deleteNoticeById?receiverId={}", id);
        if (lifeNoticeMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @GetMapping("/readNoticeById")
    @ApiOperation("通知已读")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "query")})
    public R<Boolean> readNoticeById(Integer id) {
        log.info("LifeNoticeController.readNoticeById?receiverId={}", id);
        if (lifeNoticeService.readNoticeById(id) > 0) {
            return R.success("已读成功");
        }
        return R.fail("已读失败");
    }

    @PostMapping("/addNotice")
    public R<Boolean> addNotice(LifeNotice notice) {
        log.info("LifeNoticeController.addNotice?notice={}", notice.toString());
        lifeNoticeUtil.fillUserTypeAndRefId(notice);
        if (lifeNoticeMapper.insert(notice) > 0) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("通知详情")
    @GetMapping("/getNoticeById")
    public R<LifeNotice> getNoticeById(@RequestParam String id) {
        log.info("LifeMessageController.getNoticeById?Id={}", id);
        return R.data(lifeNoticeMapper.selectById(id));
    }

}
