package cn.pluss.platform.controller.merchantGuide;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MerchantGuide;
import cn.pluss.platform.entity.MerchantQrcode;
import cn.pluss.platform.merchantGuide.MerchantGuideService;
import cn.pluss.platform.util.OssOperatUtil;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-10-23
 */
@Controller
@RequestMapping("/merchantGuide")
public class MerchantGuideController extends BaseModelController<MerchantGuideService, MerchantGuide> {


    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index() {
        return "merchantGuide/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) {
            MerchantGuide qrcode = new MerchantGuide().setId(id);
            qrcode = baseService.getOne(new QueryWrapper<>(qrcode));
            request.setAttribute("entity", qrcode);
        }
        return "merchantGuide/detail";
    }

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

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody MerchantGuide entity) {
        if(StringUtil.isEmpty(entity.getMerchantCode())){
            return ResultGenerator.genFailResult("商户编号不能为空", null);
        }
        if(StringUtil.isEmpty(entity.getMerchantName())){
            return ResultGenerator.genFailResult("商户名称不能为空", null);
        }
        if(StringUtil.isEmpty(entity.getName())){
            return ResultGenerator.genFailResult("引导话术不能为空", null);
        }
        if(StringUtil.isEmpty(entity.getStatus())){
            return ResultGenerator.genFailResult("状态不能为空", null);
        }
        if(!"0".equals(entity.getStatus()) && StringUtil.isNotEmpty(entity.getId())){
            QueryWrapper<MerchantGuide> queryWrapper = new QueryWrapper<MerchantGuide>().eq("merchantCode",entity.getMerchantCode())
                    .eq("status",entity.getStatus())
                    .ne("id",entity.getId());
            List<MerchantGuide> list = this.baseService.list(queryWrapper);
            if(list.size() != 0){
                return ResultGenerator.genFailResult("只允许有一条选中的话术！", null);
            }
        }
        return super.save(entity);
    }
}
