package com.fdkankan.ucenter.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.SceneApply;
import com.fdkankan.ucenter.service.ISceneApplyService;
import com.fdkankan.ucenter.common.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-15
 */
@RestController
@RequestMapping("/ucenter/demo/scene")
public class SceneApplyController {
    @Autowired
    private ISceneApplyService sceneApplyService;

    /**
     * 新增演示场景申请
     */
    @PostMapping("/save")
    public ResultData save(@RequestBody SceneApply param){
        if(StringUtils.isEmpty(param.getName()) || StringUtils.isEmpty(param.getPhone())
         || StringUtils.isEmpty(param.getCompany()) || StringUtils.isEmpty(param.getJob())
        || StringUtils.isEmpty(param.getCountry())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        if(param.getName().length() >50 || param.getCompany().length() >200){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3036, LoginConstant.FAILURE_MSG_3036);
        }
        sceneApplyService.save(param);
        return ResultData.ok();
    }

    @PostMapping("/pageList")
    public ResultData pageList(@RequestBody JSONObject param){
        Integer page =param.get("pageNum") == null ? 1 : param.getInteger("pageNum");
        Integer pageSize =param.get("pageSize") == null ? 10 : param.getInteger("pageSize");
        return ResultData.ok(sceneApplyService.page(new Page<>(page,pageSize)));
    }

    @PostMapping("/delete")
    public ResultData delete(@RequestBody JSONObject param){
        if(param.get("id") == null){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        sceneApplyService.removeById(param.getInteger("id"));
        return ResultData.ok();
    }
}

