package com.huimv.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultUtil;
import com.huimv.admin.entity.BasePigpen;
import com.huimv.admin.entity.dto.BasePigpenDto;
import com.huimv.admin.service.IBasePigpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/basePigpen")
@CrossOrigin
public class BasePigpenController {
    @Autowired
    private IBasePigpenService basePigpenService;

    @PostMapping( "/addPigpen")
    public Result addPigpen(@RequestBody BasePigpenDto basePigpenDto) {
        return basePigpenService.addPigpen(basePigpenDto);
    }

    @PostMapping( "/addUnit")
    public Result addUnit(@RequestBody BasePigpen basePigpen) {
        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", basePigpen.getFarmId()).eq("f_type", 3).eq("build_name", basePigpen.getBuildName());
        BasePigpen pigpen = basePigpenService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(pigpen)) {
            return ResultUtil.addResult(0);
        }
        BasePigpen byId = basePigpenService.getById(basePigpen.getParentId());
        String parentIds = byId.getOther2() + "," + basePigpen.getParentId();
        basePigpen.setFType(3);
        basePigpen.setOther2(parentIds);
        if ( basePigpenService.save(basePigpen)){
            return ResultUtil.addResult(1);
        }
        return ResultUtil.addResult(0);
    }

    @PostMapping( "/updatePigpen")
    public Result updatePigpen(@RequestBody BasePigpen basePigpen) {
         return  basePigpenService.updatePigpen(basePigpen);
    }

    @PostMapping( "/updateUnit")
    public Result updateUnit(@RequestBody BasePigpen basePigpen) {
        Integer  num = basePigpenService.count(new QueryWrapper<BasePigpen>().eq("build_name", basePigpen.getBuildName())
                .eq("farm_id",basePigpen.getFarmId()).ne("id", basePigpen.getId()));
        if (num  > 0){
            return new Result(10001, "栋舍名称已存在！", false);
        }
        if ( basePigpenService.updateById(basePigpen)){
            return ResultUtil.addResult(1);
        }
        return ResultUtil.addResult(0);
    }

    @PostMapping( "/deletePigpen")
    public Result deletePigpen(@RequestBody Map<String,Integer> map) {
        return  basePigpenService.deletePigpen(map);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result list(@RequestBody Map<String, Object> paramsMap) {
        System.out.println("paramsMap>>"+paramsMap.toString());
        String farmCode = "";
        if (paramsMap.get("farmId") != null) {
            farmCode = paramsMap.get("farmId")+"";
        }
        String stageCode = "";
        if (paramsMap.get("stageCode") != null) {
            stageCode = paramsMap.get("stageCode")+"";
        }
        String buildName = "";
        if (paramsMap.get("buildName") != null) {
            buildName = paramsMap.get("buildName")+"";
        }
        return basePigpenService.list(farmCode,buildName,stageCode);
    }
}
