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.controller.feedVo.FeedVo;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.EnergyFeed;
import com.huimv.admin.entity.PigUnit;
import com.huimv.admin.entity.PjData;
import com.huimv.admin.service.IEnergyDataService;
import com.huimv.admin.service.IEnergyFeedService;
import com.huimv.admin.service.IPigUnitService;
import com.huimv.admin.service.IPjDataService;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/receive")
@CrossOrigin
public class ReceiveController {
    @Autowired
    private IEnergyFeedService energyFeedService;
    @Autowired
    private IPjDataService pjDataService;
    @Autowired
    private IPigUnitService pigUnitService;
    @Autowired
    private IEnergyDataService dataService;

    @PostMapping("/feed")
    public Result list(HttpServletRequest httpServletRequest, @RequestBody FeedVo feedVo) throws ParseException {
        try {
            if (ObjectUtil.isEmpty(feedVo)) {
                return new Result(10002, "数据格式不正确", false);
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(feedVo.getDateTime())
                    .append(feedVo.getFarmName())
                    .append(feedVo.getFarmId())
                    .append(feedVo.getPigStyName())
                    .append(feedVo.getPigStyId())
//                    .append(feedVo.getFloorName())
                    .append(feedVo.getFeedName())
//                    .append(feedVo.getFloorId())
                    .append(feedVo.getWeight());
            String mac = feedVo.getMac();
            String s = DigestUtils.md5DigestAsHex(stringBuilder.toString().getBytes()).toLowerCase();
            System.out.println(s);
            if (!s.equals(mac)) {
                return new Result(10001, "序列校验不通过", false);
            }
            System.out.println(feedVo.toString());
            String id = feedVo.getId();
            if (StringUtils.isBlank(id)) {
                return new Result(10004, "唯一字段ID为空", false);
            }
            int ids = energyFeedService.count(new QueryWrapper<EnergyFeed>().eq("IDS", id));
            if (ids > 0) {
                return new Result(10005, "该数据已上传，请勿重复上传", false);
            }
            System.out.println();
            EnergyFeed energyFeed = new EnergyFeed();
            String dateTime = feedVo.getDateTime();
            SimpleDateFormat st = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            energyFeed.setCreateDate(st.parse(dateTime));
            energyFeed.setFarmId(23);
            energyFeed.setFeedValue(feedVo.getWeight());
            energyFeed.setOriginalDate(feedVo.getWeight());
            energyFeed.setOriginalValue(feedVo.getWeight());
            energyFeed.setUnitId(225);
            energyFeed.setIds(id);
            energyFeedService.save(energyFeed);

            PjData data = new PjData();
            data.setDateTime(st.parse(dateTime));
            data.setFarmId("23");
            data.setFarmName("浦江");
            data.setPigStyId(feedVo.getPigStyId());
            data.setPigStyName(feedVo.getPigStyName());
            data.setFeedName(feedVo.getFeedName());
            data.setWeight(feedVo.getWeight());
            pjDataService.save(data);


            EnergyData energyData = new EnergyData();
            energyData.setElectricityValue("0");
            energyData.setWaterValue("0");
            energyData.setGasValue("0");
            energyData.setUnitId(225);
            energyData.setFarmId(23);
            energyData.setCreateDate(new Date());
            energyData.setFeedValue(feedVo.getWeight());
            dataService.save(energyData);

            return new Result(10000, "操作成功", true);
        } catch (Exception e) {
            System.out.println(e);
            return new Result(10003, "其它错误", false);
        }

    }

    public static void main(String[] args) {
        String s = "10010203";
        System.out.println(s.substring(0, 4));
        System.out.println(s.substring(4, 6));
        System.out.println(s.substring(6, 8));
    }

    @PostMapping("/update")
    public Result update(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<EnergyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        List<EnergyData> list = dataService.list(queryWrapper);
        for (EnergyData energyData : list) {
            energyData.setFeedValue("0");
            dataService.updateById(energyData);
        }
        return null;
    }
}
