package com.huimv.eartag2.admin.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.eartag2.admin.service.BasePigpenService;
import com.huimv.eartag2.common.utils.Result;
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 zn
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/base-pigpen")
public class BasePigpenController {
    @Autowired
    private BasePigpenService basePigpenService;

    @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 pigpenName = "";
        if (paramsMap.get("pigpenName") != null) {
            pigpenName = paramsMap.get("pigpenName")+"";
        }
        return basePigpenService.list(farmCode,pigpenName,stageCode);
    }



    // 添加栋舍
    /**
     * @Method      : addBuilding
     * @Description :  farmCode,buildingName,buildingCode,sort --以上必填
     * @Params      : [paramsMap]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/7/23
     * @Time        : 15:16
     */
    @RequestMapping(value = "/addBuilding",method = RequestMethod.POST)
    public Result addBuilding(@RequestBody Map<String, Object> paramsMap){
        // 牧场名称
        String farmCode = paramsMap.get("farmId")+"";
        // 栋名称
        String buildingName = paramsMap.get("buildingName")+"";

        // 栋编码
//        String buildingCode = paramsMap.get("buildingCode")+"";
        String buildingCode = System.currentTimeMillis()+"" ;
        // 阶段
        String stageCode = paramsMap.get("stageCode")+"";
        // 排序
        String sort = paramsMap.get("sort")+"";
        if(StringUtils.isBlank(sort)){
            sort = "1";
        }
        // 类型
        Integer type = 1;
        //
        return basePigpenService.addBuilding(farmCode,buildingName,buildingCode,stageCode,Integer.parseInt(sort),type);
    }

    @RequestMapping(value = "/editBuilding",method = RequestMethod.POST)
    public Result editBuilding(@RequestBody Map<String, Object> paramsMap){
        // id
        String id = paramsMap.get("id")+"";
        // 栋名称
        String buildingName = paramsMap.get("buildingName")+"";
        // 栋编码
//        String buildingCode = paramsMap.get("buildingCode")+"";
        // 阶段
        String stageCode = paramsMap.get("stageCode")+"";
        // 排序
        String sort = paramsMap.get("sort")+"";
        if(StringUtils.isBlank(sort)){
            sort = "1";
        }
        // 类型
        Integer type = 1;
        //
        return basePigpenService.editBuilding(id,buildingName,stageCode,Integer.parseInt(sort),type);
    }

    // 删除栋舍
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    public Result remove(@RequestBody String ids){
        //
        return basePigpenService.remove(ids);
    }

    // 添加单元
    @RequestMapping(value = "/addUnit",method = RequestMethod.POST)
    public Result addUnit(@RequestBody Map<String, Object> paramsMap){
        // 牧场名称
        String farmCode = paramsMap.get("farmId")+"";
        // 栋
        String parentId = paramsMap.get("parentId")+"";
        // 单元
//        String unitCode = paramsMap.get("unitCode")+"";
        String unitCode = System.currentTimeMillis()+"" ;
        // 单元
        String unitName = paramsMap.get("unitName")+"";
        // 阶段
        String stageCode = paramsMap.get("stageCode")+"";
        // 排序
        String sort = paramsMap.get("sort")+"";
        if(StringUtils.isBlank(sort)){
            sort = "1";
        }
        // 类型
        Integer type = 2;
        //
        return basePigpenService.addUnit(farmCode,parentId,unitName,unitCode,stageCode,Integer.parseInt(sort),type);
    }

    @RequestMapping(value = "/editUnit",method = RequestMethod.POST)
    public Result editUnit(@RequestBody Map<String, Object> paramsMap){
        // id
        String id = paramsMap.get("id")+"";
        // 栋
        String parentId = paramsMap.get("parentId")+"";
        // 单元
//        String unitCode = paramsMap.get("unitCode")+"";
        // 单元
        String unitName = paramsMap.get("unitName")+"";
        // 阶段
        String stageCode = paramsMap.get("stageCode")+"";
        // 排序
        String sort = paramsMap.get("sort")+"";
        if(StringUtils.isBlank(sort)){
            sort = "1";
        }
        // 类型
        Integer type = 2;
        //
        return basePigpenService.editUnit(id,parentId,unitName,stageCode,Integer.parseInt(sort),type);
    }

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

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

}

