package cn.pluss.platform.controller.merchantMarket;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MerchantMarket;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.merchantMarket.MerchantMarketService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 商户奖励金记录表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-11-02
 */
@Controller
@RequestMapping("/merchantMarket")
public class MerchantMarketController extends BaseModelController<MerchantMarketService, MerchantMarket> {

    @Autowired
    private UserAppService userAppService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index() {
        return "merchantMarket/list";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            MerchantMarket market = new MerchantMarket().setId(id);
            market = baseService.getOne(new QueryWrapper<>(market));
            request.setAttribute("entity", market);
        }
        return "merchantMarket/detail";
    }

    @Override
    public Result<Page<MerchantMarket>> page(Page<MerchantMarket> page, MerchantMarket condition, OrderItem orderItem) {
        orderItem.setColumn("createTime");
        orderItem.setAsc(false);
        return super.page(page, condition, orderItem);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody MerchantMarket entity) {
        if(StringUtil.isEmpty(entity.getUserId())){
            return ResultGenerator.genFailResult("用户ID不能为空");
        }
        if(StringUtil.isEmpty(entity.getSource())){
            return ResultGenerator.genFailResult("来源不能为空");
        }
        if(StringUtil.isEmpty(entity.getMarketAmt())){
            return ResultGenerator.genFailResult("积分不能为空");
        }
        QueryWrapper<UserApp> queryWrapper = new QueryWrapper<UserApp>().
                eq("userId",entity.getUserId());
        UserApp app = userAppService.getOne(queryWrapper);
        if(app == null){
            return ResultGenerator.genFailResult("请输入正确的用户ID");
        }
        if(StringUtil.isEmpty(app.getMerchantCode())){
            return ResultGenerator.genFailResult("当前用户不支持增加积分");
        }
        entity.setUserName(app.getUserName());
        entity.setCreateTime(new Date());
        return super.save(entity);
    }
}
