package shop.alien.second.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondShield;
import shop.alien.entity.second.vo.SecondGoodsVo;
import shop.alien.mapper.second.SecondShieldMapper;
import shop.alien.util.common.JwtUtil;

import java.util.Date;

@Slf4j
@Api(tags = {"二手平台-拉黑"})
@ApiSort(9)
@CrossOrigin
@RestController
@RequestMapping("/second/shield")
@RequiredArgsConstructor
public class SecondShieldController {

    private final SecondShieldMapper mapper;

    @ApiOperation("商品拉黑")
    @PostMapping("/saveGoodsShield")
    public R<T> saveGoodsShield(
            @RequestParam(value = "shieldId", required = false) Integer shieldId) {
        JSONObject data = JwtUtil.getCurrentUserInfo();
        Integer userId = null;
        if (data != null) {
            userId = data.getInteger("userId");
        }
        if (userId == null) {
            return null;
        }
        SecondShield shield = new SecondShield();
        shield.setUserId(userId);
        shield.setShieldId(shieldId);
        shield.setShieldType(1);
        shield.setCreatedUserId(userId);
        shield.setCreatedTime(new Date());
        shield.setUpdatedUserId(userId);
        shield.setUpdatedTime(new Date());
        Integer num = mapper.insert(shield);

        if(num == 1) {
            return R.success("拉黑成功");
        }
        return R.fail("拉黑失败");

    }


    /**
     * 取消拉黑
     */
    @PostMapping("/deleteGoodsShield")
    @ApiOperation("取消拉黑")
    public R<Void> deleteGoodsShield(@ApiParam("取消拉黑") @RequestBody SecondGoodsVo secondGoods) {
        log.info("SecondShieldController.GoodsShield?secondGoods={}", secondGoods.toString());
        mapper.deleteById(secondGoods.getShieldId());
        return R.success("取消拉黑成功");
    }

}
