package com.huimv.receive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
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.common.utils.ResultUtil;
import com.huimv.receive.entity.BaseLocation;
import com.huimv.receive.entity.BillSampling;
import com.huimv.receive.entity.Exist;
import com.huimv.receive.service.IBaseFarmService;
import com.huimv.receive.service.IBaseLocationService;
import com.huimv.receive.service.IExistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/base-location")
@CrossOrigin
public class BaseLocationController {
    @Autowired
    private IBaseLocationService baseLocationService;
    @Autowired
    private IBaseFarmService baseFarmService;

    @PostMapping("/listAll")
    public Result listAll(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        return new Result(ResultCode.SUCCESS,baseLocationService.list(new QueryWrapper<BaseLocation>().lambda()
                .like(BaseLocation::getFarmIds,farmId)));
    }

    @PostMapping("/page")
    public Result page(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<BaseLocation> page = new Page<>(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        Page<BaseLocation> baseLocationPage = baseLocationService.page(page, new QueryWrapper<BaseLocation>().lambda()
                .like(BaseLocation::getFarmIds, farmId));
        for (BaseLocation record : baseLocationPage.getRecords()) {
            String[] strings = record.getFarmIds().split(",");
            List farmNameList = new ArrayList();
            for (String farm : strings) {
                farmNameList.add(baseFarmService.getById(farm).getFarmName());
            }
            record.setFarmNames(String.join(",",farmNameList));
        }
        return new Result(ResultCode.SUCCESS, baseLocationPage);
    }

    @PostMapping("/listLuggageLocation")
    public Result listLuggageLocation(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        return new Result(ResultCode.SUCCESS, baseLocationService.list(new QueryWrapper<BaseLocation>()
                .eq("is_luggage", 1).like("farm_Ids", farmId)));
    }

    @PostMapping("/add")
    public Result add(HttpServletRequest httpServletRequest, @RequestBody BaseLocation baseLocation){
        return baseLocationService.add(httpServletRequest,baseLocation);
    }

    @PostMapping("/edit")
    public Result edit(HttpServletRequest httpServletRequest, @RequestBody BaseLocation baseLocation){
        return baseLocationService.edit(httpServletRequest,baseLocation);
    }

    @PostMapping("/delete")
    public Result delete(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap){
        return baseLocationService.delete(httpServletRequest,paramsMap);
    }
}
