package com.hzs.retail.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.base.BdNotice;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.retail.vo.NoticeListVO;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.base.service.IBdNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 新零售公告控制器
 * @Author: jiang chao
 * @Time: 2024/12/9 13:40
 * @Classname: ApiRetailNoticeController
 * @PackageName: com.hzs.retail.controller
 */
@Slf4j
@RestController
@RequestMapping("/api/retail-notice")
public class ApiRetailNoticeController extends BaseController {

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    @Autowired
    private IBdNoticeService iBdNoticeService;

    /**
     * 公告列表
     *
     * @return
     */
    @GetMapping("/notice-list")
    public TableDataInfo noticeList() {
        List<NoticeListVO> resultList = new ArrayList<>();
        Integer systemType = SecurityUtils.getSystemType();
        Integer pkAwards = iMemberServiceApi.getMemberById(SecurityUtils.getUserId()).getData().getPkAwards();
        startPage();
        List<BdNotice> noticeList = iBdNoticeService.listRetailData(pkAwards, SecurityUtils.getPkCountry(),systemType);

        if (CollectionUtil.isNotEmpty(noticeList)) {
            resultList = BeanUtil.copyToList(noticeList, NoticeListVO.class);
        }

        TableDataInfo tableDataInfo = getDataTable(noticeList);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

    /**
     * 公告弹窗
     *
     * @return
     */
    @GetMapping("/notice-pop")
    public AjaxResult noticePop() {
        Integer pkAwards = iMemberServiceApi.getMemberById(SecurityUtils.getUserId()).getData().getPkAwards();
        BdNotice bdNotice = iBdNoticeService.getRetailLast(pkAwards, SecurityUtils.getPkCountry(),SecurityUtils.getSystemType());
        if (null != bdNotice) {
            return AjaxResult.success(BeanUtil.copyProperties(bdNotice, NoticeListVO.class));
        }
        return AjaxResult.success();
    }

    /**
     * 公告详情
     *
     * @param pkId 公告ID
     * @return
     */
    @GetMapping("/notice-detail")
    public AjaxResult noticeDetail(@RequestParam("pkId") Long pkId) {
        BdNotice bdNotice = iBdNoticeService.getById(pkId);
        return AjaxResult.success(BeanUtil.copyProperties(bdNotice, NoticeListVO.class));
    }

}
