
package com.tmerclub.cloud.multishop.controller.admin;

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.multishop.constant.NoticeType;
import com.tmerclub.cloud.multishop.dto.NoticeDTO;
import com.tmerclub.cloud.multishop.model.Notice;
import com.tmerclub.cloud.multishop.service.NoticeService;
import com.tmerclub.cloud.multishop.vo.NoticeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公告
 * @author YXF
 * @date 2020-11-25 17:57:56
 */
@RestController("adminNoticeController")
@RequestMapping("/mp/notice")
@Tag(name = "admin-公告")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    @GetMapping("/page")
    @Operation(summary = "获取公告列表", description = "分页获取公告列表")
    public ServerResponseEntity<PageVO<NoticeVO>> page(@Valid PageDTO pageDTO, NoticeDTO noticeDTO) {
        noticeDTO.setShopId(AuthUserContext.get().getTenantId());
        noticeDTO.setAccountId(AuthUserContext.get().getTenantId());
        PageVO<NoticeVO> noticePage = noticeService.page(pageDTO, noticeDTO);
        return ServerResponseEntity.success(noticePage);
    }

    @GetMapping("/p/pagePlatformNotice")
    @Operation(summary = "获取平台公告分页列表（商家端或供应商端）", description = "获取平台公告分页列表（商家端或供应商端）")
    public ServerResponseEntity<PageVO<NoticeVO>> pagePlatformNotice(@Valid PageDTO pageDTO, NoticeDTO noticeDTO) {
        Integer sysType = AuthUserContext.get().getSysType();
        noticeDTO.setShopId(Constant.PLATFORM_SHOP_ID);
        noticeDTO.setType(Objects.equals(sysType, SysTypeEnum.MULTISHOP.value()) ? NoticeType.TO_MULTISHOP.value() : NoticeType.TO_SUPPLIER.value());
        noticeDTO.setAccountId(AuthUserContext.get().getTenantId());
        noticeDTO.setSendTime(new Date());
        noticeDTO.setStatus(StatusEnum.ENABLE.value());
        PageVO<NoticeVO> notices = noticeService.pagePlatformNotice(pageDTO, noticeDTO);

        return ServerResponseEntity.success(notices);
    }

    @GetMapping
    @Operation(summary = "获取公告", description = "根据id获取公告")
    public ServerResponseEntity<NoticeVO> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(noticeService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存公告", description = "保存公告")
    public ServerResponseEntity<Void> save(@Valid @RequestBody NoticeDTO noticeDTO) {
        Notice notice = BeanUtil.map(noticeDTO, Notice.class);
        Long shopId = AuthUserContext.get().getTenantId();
        //商家端创建公告
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            notice.setTypes(NoticeType.TO_USER.value().toString());
            notice.setImmediatelySend(1);
        }
        notice.setShopId(shopId);
        noticeService.save(notice);
        noticeService.removeCacheByShopIdAndType(shopId, NoticeType.TO_USER.value());
        removeCache(notice.getTypes(), shopId);

        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新公告", description = "更新公告")
    public ServerResponseEntity<Void> update(@Valid @RequestBody NoticeDTO noticeDTO) {
        Notice notice = BeanUtil.map(noticeDTO, Notice.class);
        Long shopId = AuthUserContext.get().getTenantId();
        NoticeVO dbNotice = noticeService.getById(notice.getId());
        if (!Objects.equals(dbNotice.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        boolean isPublish = Objects.equals(dbNotice.getStatus(), StatusEnum.DISABLE.value()) && Objects.equals(notice.getStatus(), StatusEnum.ENABLE.value());
        if (isPublish) {
            notice.setPublishTime(new Date());
        } else {
            notice.setPublishTime(null);
        }

        noticeService.update(notice);
        removeCache(notice.getTypes(), shopId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除公告", description = "根据公告id删除公告")
    @Parameter(name = "id", description = "公告id", required = true)
    public ServerResponseEntity<Void> delete(@RequestParam("id") Long id) {
        NoticeVO dbNotice = noticeService.getById(id);
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.isNull(dbNotice)) {
            return ServerResponseEntity.success();
        } else if (!Objects.equals(dbNotice.getShopId(), shopId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        noticeService.deleteById(id);
        removeCache(dbNotice.getTypes(), shopId);
        return ServerResponseEntity.success();
    }

    private void removeCache(String dbNotice, Long shopId) {
        List<Integer> typeList = Arrays.stream(dbNotice.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        for (Integer type : typeList) {
            noticeService.removeCacheByShopIdAndType(shopId, type);
        }
    }
}
