/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AttachFile;
import com.yami.shop.bean.model.IndexImg;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.AttachFileService;
import com.yami.shop.service.IndexImgService;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;
import com.yami.shop.common.util.R;
/**
 * 首页图片管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/platform/indexImg")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;
    @Autowired
    private ProductService productService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:indexImg:page')")
    public R<IPage<IndexImg>> page(IndexImg indexImg, PageParam<IndexImg> page) {
        IPage<IndexImg> indexImgIPage = indexImgService.page(page,
                new LambdaQueryWrapper<IndexImg>()
                        .eq(indexImg.getStatus() != null, IndexImg::getStatus, indexImg.getStatus())
                        .eq(IndexImg::getShopId, Constant.PLATFORM_SHOP_ID)
                        .eq(!Objects.isNull(indexImg.getImgType()),IndexImg::getImgType, indexImg.getImgType())
                        .orderByDesc(IndexImg::getStatus)
                        .orderByAsc(IndexImg::getImgType)
                        .orderByDesc(IndexImg::getSeq));
        return R.SUCCESS(indexImgIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{imgId}")
    @PreAuthorize("@pms.hasPermission('platform:indexImg:info')")
    public R<IndexImg> info(@PathVariable("imgId") Long imgId) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        IndexImg indexImg = indexImgService.getOne(new LambdaQueryWrapper<IndexImg>().eq(IndexImg::getShopId, shopId).eq(IndexImg::getImgId, imgId));
        if (Objects.nonNull(indexImg.getRelation())) {
            Product product = productService.getProductByProdId(indexImg.getRelation());
            if (product !=null) {
                indexImg.setPic(product.getPic());
                indexImg.setProdName(product.getProdName());
            }
        }
        return R.SUCCESS(indexImg);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:indexImg:save')")
    public R<Void> save(@RequestBody @Valid IndexImg indexImg) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        indexImg.setShopId(shopId);
        indexImg.setUploadTime(new Date());
        indexImgService.save(indexImg);
        indexImgService.removeIndexImgCacheByShopId(shopId);
        return R.SUCCESS();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:indexImg:update')")
    public R<Void> update(@RequestBody @Valid IndexImg indexImg) {
        indexImgService.saveOrUpdate(indexImg);
        // 移除缓存
        indexImgService.removeIndexImgCacheByShopId(Constant.PLATFORM_SHOP_ID);
        return R.SUCCESS();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform:indexImg:delete')")
    public R<Void> delete(@RequestBody Long[] ids) {
        indexImgService.deleteIndexImgsByIds(ids, Constant.PLATFORM_SHOP_ID);
        // 移除缓存
        indexImgService.removeIndexImgCacheByShopId(Constant.PLATFORM_SHOP_ID);
        return R.SUCCESS();
    }

}
