package com.fzm.mall.server.front.asset.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.front.asset.model.dto.UserAssetTransferRecordSaveDTO;
import com.fzm.mall.server.front.asset.model.vo.UserAssetTransferRecordVO;
import com.fzm.mall.server.front.asset.service.IUserAssetTransferRecordService;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.repo.ResponseFactory;
import com.fzm.mall.server.front.user.model.Header;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户资产-转赠 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-08-17
 */
@Api(tags = "我的仓库-转赠")
@RestController
@RequestMapping("/mall/transferRecord")
public class UserAssetTransferRecordController {

    @Autowired
    private IUserAssetTransferRecordService transferRecordService;

    @Autowired
    private ResponseFactory resFac;

    @ApiOperation(value = "转赠-新增")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    public ResponseVO save(@Ignore @RequestAttribute Header header, @RequestBody @Valid UserAssetTransferRecordSaveDTO saveDTO) {
        saveDTO.setUid(header.getUid());
        try{
            transferRecordService.save(saveDTO);
        }catch (Exception e){
            if("addr_invalid".equals(e.getMessage())){
                return  new ResponseVO<>(MallResponseEnum.TRAN_RECORD_FAIL);
            }else{
                throw e;
            }
        }
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "转赠-主键查询")
    @ApiOperationSupport(order = 5)
    @PostMapping("/{transferRecordId}")
    public ResponseVO read(
            @ApiParam(value = "transferRecordId", required = true) @PathVariable(value = "transferRecordId") String transferRecordId) {
        UserAssetTransferRecordVO result = transferRecordService.read(transferRecordId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "转赠-列表查询")
    @ApiOperationSupport(order = 6)
    @PostMapping("/list")
    public ResponseVO list(@Ignore @RequestAttribute Header header,
                           @ApiParam(value = "商品编号", required = true) @RequestParam String goodsId,
                           @ApiParam(value = "sku编号", required = true) @RequestParam String skuId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long size,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long page) {
        Page<UserAssetTransferRecordVO> iPage = new Page<>();
        if (ObjectUtils.isNotEmpty(size) && ObjectUtils.isNotEmpty(page)) {
            iPage.setSize(size);
            iPage.setCurrent(page);
        }

        IPage<UserAssetTransferRecordVO> result = transferRecordService.page(iPage, header.getUid(), goodsId, skuId);
        return resFac.getSuccessResponseWithData(result);
    }
}
