package com.huimv.receive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BaseFlow;
import com.huimv.receive.entity.BaseVisiting;
import com.huimv.receive.entity.BaseWashoutPoint;
import com.huimv.receive.service.IBaseVisitingService;
import com.huimv.receive.service.IBaseWashoutPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-06
 */
@RestController
@RequestMapping("/base-visiting")
@CrossOrigin
public class BaseVisitingController {

    @Autowired
    private IBaseVisitingService baseVisitingService;
    @Autowired
    private IBaseWashoutPointService baseWashoutPointService;

    @PostMapping("/add")
    public Result add(HttpServletRequest httpServletRequest, @RequestBody BaseVisiting baseVisiting){
        return baseVisitingService.add(httpServletRequest,baseVisiting);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,String> paramsMap){
        String id = paramsMap.get("id");
        if (baseWashoutPointService.count(new QueryWrapper<BaseWashoutPoint>().lambda().eq(BaseWashoutPoint::getVisitingType,id)) > 0) {
            return new Result(10001,"请先删除该来访类型相关洗消点!",false);
        }
        return baseVisitingService.delete(id);
    }

    @PostMapping("/page")
    public Result page(@RequestBody Map<String,String> paramsMap){
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<BaseVisiting> baseVisitingPage = new Page<>(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS,baseVisitingService.page(baseVisitingPage,new QueryWrapper<>()));
    }

    @PostMapping("/list")
    public Result list(@RequestBody Map<String,String> paramsMap){
        return new Result(ResultCode.SUCCESS,baseVisitingService.list(new QueryWrapper<>()));
    }


}
