package com.liquidnet.client.admin.web.controller.zhengzai.candy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.zhengzai.candy.service.ICandyCouponAdminService;
import com.liquidnet.client.admin.zhengzai.candy.service.ICandyCouponRuleAdminService;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.candy.dto.admin.CandyCouponListParam;
import com.liquidnet.service.candy.dto.admin.CandyCouponTemplateDto;
import com.liquidnet.service.candy.entity.CandyCoupon;
import com.liquidnet.service.candy.entity.CandyCouponRule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(tags = "我的券包")
@Controller
@RequestMapping("candy/coupon")
public class CandyCouponAdminController extends BaseController {

    @Autowired
    ICandyCouponAdminService candyCouponAdminService;
    @Autowired
    ICandyCouponRuleAdminService candyCouponRuleAdminService;

    @ApiOperation(value = "券列表")
    @PostMapping("list")
    @ResponseBody
    public String list(CandyCouponListParam listParam) {
        LambdaQueryWrapper<CandyCoupon> queryWrapper = Wrappers.lambdaQuery(CandyCoupon.class);
        queryWrapper.select(CandyCoupon::getCouponId, CandyCoupon::getTitle);
        queryWrapper.orderByDesc(CandyCoupon::getCreatedAt);

        if (null != listParam) {
            if (StringUtils.isNotBlank(listParam.getKeyword())) {
                queryWrapper.like(CandyCoupon::getTitle, listParam.getKeyword());
            }
            if (null != listParam.getBusiType()) {
                queryWrapper.eq(CandyCoupon::getBusiType, listParam.getBusiType());
            }
            if (null != listParam.getCouType()) {
                queryWrapper.eq(CandyCoupon::getCouType, listParam.getCouType());
            }
            if (null != listParam.getBindType()) {
                queryWrapper.eq(CandyCoupon::getBindType, listParam.getBindType());
            }
        }

        startPage();
        return JsonUtils.toJson(getDataTable(candyCouponAdminService.list(queryWrapper)));
    }

    @ApiOperation(value = "券详情")
    @GetMapping("info")
    @ResponseBody
    public AjaxResult info(String couponId) {
        CandyCouponTemplateDto couponTemplateDto = CandyCouponTemplateDto.getNew();

        LambdaQueryWrapper<CandyCoupon> couponLambdaQueryWrapper = Wrappers.lambdaQuery();
        couponLambdaQueryWrapper.eq(CandyCoupon::getCouponId, couponId);
        couponLambdaQueryWrapper.eq(CandyCoupon::getState, 1);
        CandyCoupon coupon = candyCouponAdminService.getOne(couponLambdaQueryWrapper);

        LambdaUpdateWrapper<CandyCouponRule> couponRuleLambdaUpdateWrapper = Wrappers.lambdaUpdate(CandyCouponRule.class);
        couponRuleLambdaUpdateWrapper.eq(CandyCouponRule::getCouponId, couponId);
        couponRuleLambdaUpdateWrapper.eq(CandyCouponRule::getState, 1);
        List<CandyCouponRule> couponRuleList = candyCouponRuleAdminService.list(couponRuleLambdaUpdateWrapper);

        couponTemplateDto.setUseRules(couponRuleList);
        return AjaxResult.success(couponTemplateDto.copy(coupon));
    }
}
