package cn.pluss.platform.controller.guideLabel;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.GuideLabel;
import cn.pluss.platform.guideLabel.GuideLabelService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-10-23
 */
@Controller
@RequestMapping("/guideLabel")
public class GuideLabelController extends BaseModelController<GuideLabelService, GuideLabel> {

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

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody GuideLabel entity) {
        if(StringUtil.isEmpty(entity.getName())){
            return ResultGenerator.genFailResult("话术名称不能为空", null);
        }
        if(StringUtil.isEmpty(entity.getStatus())){
            return ResultGenerator.genFailResult("状态不能为空", null);
        }
        if(!"0".equals(entity.getStatus())){
            QueryWrapper<GuideLabel> queryWrapper = new QueryWrapper<GuideLabel>();
            if(StringUtil.isEmpty(entity.getId())){
                queryWrapper.eq("status",entity.getStatus());
            }else{
                queryWrapper.eq("status",entity.getStatus()).ne("id",entity.getId());
            }
            List<GuideLabel> list = this.baseService.list(queryWrapper);
            if(list.size() != 0){
                return ResultGenerator.genFailResult("选中的数据只能有一条！", null);
            }
        }
        return super.save(entity);
    }
}
