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

package com.yami.shop.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.ProdComm;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ProdCommService;
import com.yami.shop.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 商品评论
 *
 * @author xwc
 * @date 2019-04-19 10:43:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/prod/prodComm" )
public class ProdCommController {

    private final ProdCommService prodCommService;
    private final ProductService productService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param prodComm 商品评论
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<ProdComm>> getProdCommPage(PageParam page, ProdComm prodComm) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        return ResponseEntity.ok(prodCommService.getProdCommPage(page,prodComm,shopId));
    }


    /**
     * 通过id查询商品评论
     * @param prodCommId id
     * @return 单个数据
     */
    @GetMapping("/info/{prodCommId}" )
    public ResponseEntity<ProdComm> getById(@PathVariable("prodCommId" ) Long prodCommId) {
        ProdComm prodComm = prodCommService.getById(prodCommId);
        if(!StrUtil.isBlank(prodComm.getPics())){
            prodComm.setPicsArray(prodComm.getPics().split(","));
        }
        return ResponseEntity.ok(prodComm);
    }

    /**
     * 新增商品评论
     * @param prodComm 商品评论
     * @return 是否新增成功
     */
    @SysLog("新增商品评论" )
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid ProdComm prodComm) {
        return ResponseEntity.ok(prodCommService.save(prodComm));
    }

    /**
     * 修改商品评论
     * @param prodComm 商品评论
     * @return 是否修改成功
     */
    @SysLog("修改商品评论" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid ProdComm prodComm) {
        Long shopId = productService.getById(prodComm.getProdId()).getShopId();
        if(!Objects.equals(shopId ,SecurityUtils.getShopUser().getShopId())){
            throw new GlobalException("没有权限修改此商品");
        }
        ProdComm newProdComm = new ProdComm();
        newProdComm.setProdCommId(prodComm.getProdCommId());
        newProdComm.setReplyTime(new Date());
        newProdComm.setReplyContent(prodComm.getReplyContent());
        newProdComm.setStatus(prodComm.getStatus());
        newProdComm.setReplySts(1);
        return ResponseEntity.ok(prodCommService.updateById(newProdComm));
    }

    /**
     * 通过id删除商品评论
     * @param prodCommId id
     * @return 是否删除成功
     */
    @SysLog("删除商品评论" )
    @DeleteMapping("/{prodCommId}" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long prodCommId) {
        return ResponseEntity.ok(prodCommService.removeById(prodCommId));
    }

}
