 package com.huimv.management.controller;

import java.util.Arrays;
import java.util.Map;

import com.huimv.management.entity.UnitEntity;
import com.huimv.management.entity.YearPigBaseEntity;
import com.huimv.management.service.UnitService;
import com.huimv.management.service.YearPigBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huimv.management.entity.LairageRecordEntity;
import com.huimv.management.service.LairageRecordService;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.R;



/**
 * 年猪基本
 *
 * @author yinhao
 * @version ${version}
 * @date 2021-07-14 15:11:40
 */
@RestController
@RequestMapping("management/lairagerecord")
public class LairageRecordController {
    @Autowired
    private LairageRecordService lairageRecordService;

    @Autowired
    private YearPigBaseService pigBaseService;

    @Autowired
    private UnitService unitService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = lairageRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		LairageRecordEntity lairageRecord = lairageRecordService.getById(id);

        return R.ok().put("lairageRecord", lairageRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LairageRecordEntity lairageRecord){
        if (lairageRecord == null){
            return R.error("添加失败");
        }
		try {
		    YearPigBaseEntity yearPigBaseEntity = toYearPigBase(lairageRecord);
            pigBaseService.save(yearPigBaseEntity);
            lairageRecordService.save(lairageRecord);
        }catch (Exception e){
            return R.error("添加失败");
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LairageRecordEntity lairageRecord){
        YearPigBaseEntity yearPigBaseEntity = toYearPigBase(lairageRecord);
        pigBaseService.updateById(yearPigBaseEntity);
        lairageRecordService.updateById(lairageRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        pigBaseService.removeByIds(Arrays.asList(ids));
		lairageRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    //批量入栏
    @RequestMapping("/batchLairage")
    public R batchLairage(@RequestBody LairageRecordEntity lairageRecord){
        Integer unitId = lairageRecord.getUnitId();
        UnitEntity unitEntity = unitService.getById(unitId);
        if (unitEntity != null){
            lairageRecord.setPeriodId(unitEntity.getCurrentPeriodId());
        }
        for (String eartage : lairageRecord.getEartag().split(",")) {
            lairageRecord.setEartag(eartage);
            YearPigBaseEntity yearPigBaseEntity = toYearPigBase(lairageRecord);
            pigBaseService.save(yearPigBaseEntity);
            lairageRecordService.save(lairageRecord);
        }

        return R.ok();
    }

    public YearPigBaseEntity toYearPigBase (LairageRecordEntity lairageRecord){
        YearPigBaseEntity yearPigBaseEntity = new YearPigBaseEntity();
        yearPigBaseEntity.setId(lairageRecord.getId());
        yearPigBaseEntity.setPeriodId(lairageRecord.getPeriodId());
        yearPigBaseEntity.setOutFenceTime(lairageRecord.getOutFenceTime());
        yearPigBaseEntity.setOutFenceStatus(lairageRecord.getOutFenceStatus());
        yearPigBaseEntity.setEartag(lairageRecord.getEartag());
        yearPigBaseEntity.setUnitId(lairageRecord.getUnitId());
        yearPigBaseEntity.setBirthday(lairageRecord.getBirthday());
        yearPigBaseEntity.setBreed(lairageRecord.getBreed());
        yearPigBaseEntity.setDayAge(lairageRecord.getDayAge());
        yearPigBaseEntity.setFarmId(lairageRecord.getFarmId());
        yearPigBaseEntity.setFatherEartag(lairageRecord.getFatherEartag());
        yearPigBaseEntity.setFosterStatus(lairageRecord.getFosterStatus());
        yearPigBaseEntity.setGmtCreate(lairageRecord.getGmtCreate());
        yearPigBaseEntity.setGmtModified(lairageRecord.getGmtModified());
        yearPigBaseEntity.setHealthStatus(lairageRecord.getHealthStatus());
        yearPigBaseEntity.setInFenceTime(lairageRecord.getInFenceTime());
        yearPigBaseEntity.setMotherEartag(lairageRecord.getMotherEartag());
        yearPigBaseEntity.setOriginPlace(lairageRecord.getOriginPlace());
        yearPigBaseEntity.setPigstyId(lairageRecord.getPigstyId());
        yearPigBaseEntity.setSex(lairageRecord.getSex());
        yearPigBaseEntity.setWeight(lairageRecord.getWeight());
        yearPigBaseEntity.setWhereDidYouGo(lairageRecord.getWhereDidYouGo());
        return yearPigBaseEntity;
    }

}
