 package com.huimv.management.controller;

import java.lang.reflect.InvocationTargetException;
import java.text.NumberFormat;
import java.util.*;

import com.huimv.management.entity.UnitEntity;
import com.huimv.management.entity.vo.CountOut;
import com.huimv.management.service.UnitService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.huimv.management.entity.YearPigBaseEntity;
import com.huimv.management.service.YearPigBaseService;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.R;

import javax.servlet.http.HttpServletRequest;


 /**
 * 年猪基本
 *
 * @author yinhao
 * @email yinhao@163.com
 * @date 2021-05-07 15:32:42
 */
@Api(tags = "年猪基本")
@RestController
@RequestMapping("management/yearpigbase")
public class YearPigBaseController {
    @Autowired
    private YearPigBaseService yearPigBaseService;


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

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


     /**
      * 列表
      */
     @RequestMapping("/fattenList")
     public R fattenList(@RequestParam Map<String, Object> params){
         PageUtils page = yearPigBaseService.fattenList(params);

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

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		YearPigBaseEntity yearPigBase = yearPigBaseService.getById(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody YearPigBaseEntity yearPigBase){
		yearPigBaseService.save(yearPigBase);

        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody YearPigBaseEntity yearPigBase){
        Integer unitId = yearPigBase.getUnitId();
        if (unitId == null){
            return R.error("请选择单元");
        }
        Integer currentPeriodId = unitService.getById(unitId).getCurrentPeriodId();
        if (currentPeriodId == null){
            return R.error("该单元暂无栏期");
        }
        yearPigBase.setPeriodId(currentPeriodId);
        yearPigBaseService.updateById(yearPigBase);


        return R.ok();
    }

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

        return R.ok();
    }


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

        return R.ok();
    }

    //批量出栏
    @RequestMapping("/such")
    public R such(@RequestBody Integer[] ids){

        yearPigBaseService.such(ids);
        return R.ok();
    }

     //批量出栏
     @RequestMapping("/suchApp")
     public R suchApp(String[] ids){
         yearPigBaseService.suchApp(ids);
         return R.ok();
     }

    //认养未认养比例
    @RequestMapping("/outAndInProportion")
    public R outAndInProportion(Integer formFarmId){
        //以认养
        Integer adopted = yearPigBaseService.findByFosterStatus(1,formFarmId);
        //未认养
        Integer notAdopted = yearPigBaseService.findByFosterStatus(0,formFarmId);
        Map resultMap = new HashMap();
        resultMap.put("adopted",adopted);
        resultMap.put("notAdopted",notAdopted);
        Integer total = notAdopted + adopted;
        resultMap.put("total",total);
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        String adoptedPercent =numberFormat.format((float)adopted/(float)total*100);
        String notAdoptedPercent =numberFormat.format((float)notAdopted/(float)total*100);

        resultMap.put("adoptedPercent",adoptedPercent+"%");
        resultMap.put("notAdoptedPercent",notAdoptedPercent+"%");

        return R.ok().put("data",resultMap);
    }


    /**
     * 查谱系问题，请求头中需要有ID
     */
    @RequestMapping("/queryByRelationship")
    public R queryPageByRelationship(@RequestParam Map<String, Object> params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Map<String, Object> stringObjectMap = yearPigBaseService.queryPageByRelationship(params);
        return R.ok().put("Result", stringObjectMap);
    }

    /*
    * 出栏统计,每天
    */
    @RequestMapping("/suchStatisticsDay")
    public R suchStatistics(@RequestParam Map<String, Object> params){
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");
        String farmId = (String)params.get("formFarmId");
        if (StringUtils.isBlank(farmId)) {
            return R.error("牧场信息为空");
        }
        List<CountOut> countOuts = yearPigBaseService.suchStatistics(startTime, endTime, Integer.parseInt(farmId));

        return R.ok().put("data",countOuts);
    }

    /*
     * 出栏统计,每月
     */
    @RequestMapping("/suchStatisticsMonth")
    public R suchStatisticsMonth(@RequestParam Map<String, Object> params){
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");
        String farmId = (String)params.get("formFarmId");
        if (StringUtils.isBlank(farmId)) {
            return R.error("牧场信息为空");
        }

        List<CountOut> countOuts = yearPigBaseService.suchStatisteicsMonth(startTime, endTime, Integer.parseInt(farmId));

        return R.ok().put("data",countOuts);
    }

     /**
      * 转栏
      * @param map
      * @param request
      * @return
      */
    @PostMapping("/transferPeriod")
    public R transferPeriod(@RequestBody HashMap<String,Object> map, HttpServletRequest request) {
        yearPigBaseService.transferPeriod(map,request);
        return R.ok();
    }

     /**
      * 转栏
      * @param map
      * @param request
      * @return
      */
     @PostMapping("/transferPeriodClient")
     public R transferPeriodClient(@RequestBody HashMap<String,Object> map, HttpServletRequest request) {
         yearPigBaseService.transferPeriodClient(map,request);
         return R.ok();
     }

     //查询认养,未认养,出栏,育肥
     @RequestMapping("/statisticsAll")
     public R statisticsAll(@RequestParam Map<String, Object> params){
         String formFarmId = (String) params.get("formFarmId");
         if (formFarmId == null || "".equals(formFarmId) ){
             return R.error("请选择牧场");
         }
         HashMap<String,Integer> resultMap = yearPigBaseService.statisticsAll(params);
         return R.ok().put("data",resultMap);
     }


     /**
      * 列表
      */
     @RequestMapping("/listLairage")
     public R listLairage(@RequestParam Map<String, Object> params){
         PageUtils page = yearPigBaseService.listLairage(params);

         return R.ok().put("page", page);
     }
     /**
      * 入栏删除
      */
     @RequestMapping("/deleteLairage")
     public R deleteLairage(@RequestBody Integer[] ids){
         yearPigBaseService.deleteLairage(Arrays.asList(ids));

         return R.ok();
     }

 }
