package com.starry.admin.modules.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.shop.module.entity.PlayShopArticleInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoAddVo;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoQueryVo;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoUpdateVo;
import com.starry.admin.modules.shop.service.IPlayShopArticleInfoService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 店铺文章信息Controller
 *
 * @author admin
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/shop/articleInfo")
public class PlayShopArticleInfoController {

    @Resource
    private IPlayShopArticleInfoService playShopArticleInfoService;

    /**
     * 查询店铺文章信息列表
     */
    //@PreAuthorize("@customSs.hasPermission('shop:articleInfo:list')")
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayShopArticleInfoQueryVo vo) {
        IPage<PlayShopArticleInfoEntity> list = playShopArticleInfoService.selectByPage(vo);
        return R.ok(list);
    }


    /**
     * 新增店铺文章信息
     */
    //@PreAuthorize("@customSs.hasPermission('shop:articleInfo:add')")
    @PostMapping("/create")
    public R create(@Validated @RequestBody PlayShopArticleInfoAddVo vo) {
        PlayShopArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopArticleInfoEntity.class);
        PlayShopArticleInfoEntity entity1 = playShopArticleInfoService.selectByType(entity.getArticleType());
        if (entity.getId() == null) {
            if (entity1 != null) {
                throw new CustomException("同类型的文章只能存在一个");
            }
            playShopArticleInfoService.create(entity);
        } else {
            if (entity1 != null && !entity1.getId().equals(entity.getId())) {
                throw new CustomException("同类型的文章只能存在一个");
            }
            entity.setUpdatedTime(new Date());
            playShopArticleInfoService.update(entity);
        }

        return R.ok();
    }


    /**
     * 修改店铺文章信息
     */
    //@PreAuthorize("@customSs.hasPermission('shop:articleInfo:update')")
    @PostMapping("/handleUpdateState")
    public R listByPage(@Validated @RequestBody PlayShopArticleInfoUpdateVo vo) {
        PlayShopArticleInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopArticleInfoEntity.class);
        playShopArticleInfoService.update(entity);
        return R.ok();
    }

    /**
     * 删除店铺文章信息
     */
    //@PreAuthorize("@customSs.hasPermission('shop:articleInfo:delete')")
    @Log(title = "店铺文章信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playShopArticleInfoService.deleteByIds(ids));
    }


}
