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.mapper.BasePigpenMapper;
import com.huimv.admin.service.IBasePigpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private BasePigpenMapper basePigpenMapper;

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

    @PostMapping( "/addUnit")
    public Result addUnit(@RequestBody Map<String,String> map) {
        String farmId = map.get("farmId");
        String parentId = map.get("parentId");
        int buildNum = Integer.valueOf(map.get("buildNum"));
        String stageCode = map.get("stageCode");
        BasePigpen parent = basePigpenService.getById(parentId);
        String parentIds = parent.getOther2() + "," + parentId;
        int maxBuildNum = basePigpenMapper.getMaxBuildNum(parentId);
        List<BasePigpen> basePigpenList = new ArrayList<>();
        for (int i = 1; i <= buildNum; i++) {
            int num = maxBuildNum+i;
            BasePigpen basePigpen = new BasePigpen();
            basePigpen.setBuildName(parent.getBuildName()+num+"单元");
            basePigpen.setParentId(Integer.valueOf(parentId));
            basePigpen.setFType(3);
            basePigpen.setStageCode(Integer.valueOf(stageCode));
            basePigpen.setFarmId(Integer.valueOf(farmId));
            basePigpen.setOther4(num);
            basePigpen.setOther2(parentIds);
            basePigpenList.add(basePigpen);
        }

        if ( basePigpenService.saveBatch(basePigpenList)){
            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 type = paramsMap.get("type").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,type);
    }

//    @PostMapping("/testUpdate")
//    @Transactional
//    public Result test() {
//        List<BasePigpen> list = basePigpenService.list(new QueryWrapper<BasePigpen>().lambda().gt(BasePigpen::getId, 1212).ne(BasePigpen::getParentId, 0));
//        for (BasePigpen basePigpen : list) {
//            Integer parentId = basePigpen.getParentId();
//            BasePigpen pigpen = basePigpenService.getById(parentId);
//            String buildName = pigpen.getBuildName();
//            Integer id = basePigpenService.getOne(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getFType,pigpen.getFType()).eq(BasePigpen::getFarmId,basePigpen.getFarmId()).eq(BasePigpen::getBuildName, buildName).ne(BasePigpen::getId, parentId)).getId();
//            basePigpen.setParentId(id);
//            String other2 = basePigpen.getOther2();
//            List<String> modifiedValues = new ArrayList<>();
//            String[] split = other2.split(",");
//            for (String s : split) {
//                if (!s.equals("0")){
//                    BasePigpen pigpen1 = basePigpenService.getById(s);
//                    String buildName1 = pigpen1.getBuildName();
//                    Integer id1 = basePigpenService.getOne(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getFType,pigpen1.getFType()).eq(BasePigpen::getFarmId,basePigpen.getFarmId()).eq(BasePigpen::getBuildName, buildName1).ne(BasePigpen::getId, s)).getId();
//                    modifiedValues.add(id1.toString());
//                }else {
//                    modifiedValues.add(s);
//                }
//            }
//            basePigpen.setOther2(String.join(",", modifiedValues));
//            basePigpenService.updateById(basePigpen);
//        }
//        return Result.SUCCESS();
//    }

}
