package com.izouma.nineth.web;

import com.anji.captcha.util.StringUtils;
import com.izouma.nineth.domain.AssetSellLimit;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.service.AssetSellLimitService;
import com.izouma.nineth.service.AssetService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


@RestController
@RequestMapping("/assetSellLimit")
@AllArgsConstructor
@Slf4j
public class AssetSellLimitController extends BaseController {

    private AssetSellLimitService assetSellLimitService;

    private AssetService assetService;


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<AssetSellLimit> all(@RequestBody PageQuery pageQuery) {
        return assetSellLimitService.all(pageQuery);
    }



    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public void save(@RequestBody AssetSellLimit assetSellLimit){
        if(assetSellLimit.getCollectionId() == null
                || assetSellLimit.getMinPrice() == null){
            throw new BusinessException("参数缺失");
        }

        if(assetSellLimit.getMinPrice().compareTo(BigDecimal.ZERO) <= 0){
            throw new BusinessException("最小价格必须大于0");
        }
        assetSellLimitService.save(assetSellLimit);
    }



    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/delete")
    public void delete(@RequestBody AssetSellLimit assetSellLimit){
        if(assetSellLimit.getId() == null){
            throw new BusinessException("参数缺失");
        }
        assetSellLimitService.softDelete(assetSellLimit.getId());
    }



    /**
     * 初始化低价锁定
     * @return
     */
    @PostMapping("/initLimitSellPriceData")
    @PreAuthorize("hasRole('ADMIN')")
    public synchronized String initLimitSellPriceData(){
        Integer changeCount = assetService.initLimitSellPriceData();
        return "调用成功，" + changeCount + "个藏品受影响";
    }



}
