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.BaseVisiting;
import com.huimv.receive.entity.BaseVisitingMid;
import com.huimv.receive.entity.BaseWashoutPoint;
import com.huimv.receive.service.IBaseJobService;
import com.huimv.receive.service.IBaseVisitingMidService;
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 2025-03-04
 */
@RestController
@RequestMapping("/base-visiting-mid")
@CrossOrigin
public class BaseVisitingMidController {

    @Autowired
    private IBaseVisitingMidService baseVisitingMidService;
    @Autowired
    private IBaseWashoutPointService baseWashoutPointService;

      @PostMapping("/add")
    public Result add(@RequestBody BaseVisitingMid baseVisitingMid){
        return baseVisitingMidService.add(baseVisitingMid);
    }

    @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);
        }
        baseVisitingMidService.removeById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @PostMapping("/update")
    public Result page(@RequestBody BaseVisitingMid baseVisitingMid){
        return baseVisitingMidService.update(baseVisitingMid);
    }

    @PostMapping("/list")
    public Result list(@RequestBody Map<String,String> paramsMap){
        String farmId = paramsMap.get("farmId");
        return new Result(ResultCode.SUCCESS, baseVisitingMidService.list(new QueryWrapper<BaseVisitingMid>().eq("farm_id", farmId)));
    }

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

    @PostMapping("/listByType")
    public Result listByType(@RequestBody Map<String,String> paramsMap){
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        if ("7".equals(type)) {
            type = "0";
        }
        return new Result(ResultCode.SUCCESS, baseVisitingMidService.list(new QueryWrapper<BaseVisitingMid>().eq("farm_id", farmId).eq("visiting_type",type)));
    }

}
