package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.admin.domain.Coupon;
import com.qs.mp.admin.domain.CouponPkg;
import com.qs.mp.admin.domain.param.CouponPkgParam;
import com.qs.mp.admin.domain.param.CouponPkgQueryParam;
import com.qs.mp.admin.domain.param.CouponPkgStatusParam;
import com.qs.mp.admin.domain.vo.CouponPkgVO;
import com.qs.mp.admin.service.ICouponPkgItemService;
import com.qs.mp.admin.service.ICouponPkgService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.enums.ErrorCodeEnum;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 券包管理API
 *
 * @author Cup
 * @date 2022/5/6
 */
@Api(tags = "券包管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/coupon/pkg/*")
public class CouponPkgMgrController extends BaseApiController {


    @Autowired
    private ICouponPkgService couponPkgService;


    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:add')")
    @ApiOperation("创建券包")
    public AjaxResult create(@RequestBody CouponPkgParam couponPkgParam) {

        couponPkgService.create(couponPkgParam);

        return AjaxResult.success();
    }


    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:edit')")
    @ApiOperation("更新券包")
    public AjaxResult update(@RequestBody CouponPkgParam couponPkgParam) {

        couponPkgService.updateCouponPkg(couponPkgParam);

        return AjaxResult.success();
    }

    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:list')")
    @ApiOperation("券包列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "券包列表", response = CouponPkg.class)
    )
    public TableDataInfo list(@RequestBody CouponPkgQueryParam couponPkgQueryParam) {
        startPage();
        LambdaQueryWrapper<CouponPkg> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(couponPkgQueryParam.getTitle())) {
            queryWrapper.like(CouponPkg::getTitle, couponPkgQueryParam.getTitle());
        }

        if (Objects.nonNull(couponPkgQueryParam.getStatus())) {
            queryWrapper.eq(CouponPkg::getStatus, couponPkgQueryParam.getStatus());
        }
        queryWrapper.orderByDesc(CouponPkg::getId);
        List<CouponPkg> list = couponPkgService.list(queryWrapper);
        return getDataTable(list);
    }

    @PostMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:query')")
    @ApiOperation("券包详情")
    @ApiResponses(
            @ApiResponse(code = 200, message = "券包详情", response = CouponPkgVO.class)
    )
    public AjaxResult detail(@PathVariable("id") Long id) {

        return AjaxResult.success(couponPkgService.detail(id));
    }

    @PostMapping("/status")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:on')")
    @ApiOperation("券包上下架")
    public AjaxResult status(@RequestBody CouponPkgStatusParam couponPkgStatusParam) {

        // 参数校验
        if (Objects.isNull(couponPkgStatusParam.getId()) || Objects.isNull(couponPkgStatusParam.getStatus())) {
            return error(ErrorCodeEnum.ERROR_CODE_1001);
        }

        if (couponPkgService.update(new LambdaUpdateWrapper<CouponPkg>()
                .set(CouponPkg::getStatus, couponPkgStatusParam.getStatus())
                .eq(CouponPkg::getId, couponPkgStatusParam.getId()))) {
            return AjaxResult.success("操作成功");
        }

        return AjaxResult.error("操作失败");
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('business:couponPkg:remove')")
    @ApiOperation("删除券包")
    public AjaxResult delete(@PathVariable("id") Long id) {
        couponPkgService.delete(id);
        return AjaxResult.success();
    }
}
