package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.ProdAnalysis;
import com.huimv.admin.entity.vo.ProAnalysisVo;
import com.huimv.admin.mapper.ProdAnalysisMapper;
import com.huimv.admin.service.IProdAnalysisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 重要指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@Service
public class ProdAnalysisServiceImpl extends ServiceImpl<ProdAnalysisMapper, ProdAnalysis> implements IProdAnalysisService {

    @Autowired
    private ProdAnalysisMapper prodAnalysisMapper;
    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        ProAnalysisVo proAnalysisVo = new ProAnalysisVo();
        Integer count=null;
        Integer count1=null;
        Integer count2=null;

        QueryWrapper<ProdAnalysis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.eq("farm_id", farmId).eq("type", 1).orderByDesc("creat_time")
                .last(" limit 1");
        ProdAnalysis prodAnalysis = prodAnalysisMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(prodAnalysis)) {
            ProdAnalysis prodAnalysis3 = new ProdAnalysis();
            prodAnalysis3.setZcb("0");
            prodAnalysis3.setByzq("0");
            prodAnalysis3.setFarmId(Integer.parseInt(farmId));
            prodAnalysis3.setHbzq("0");
            prodAnalysis3.setId(0);
            prodAnalysis3.setJczq("0");
            prodAnalysis3.setType(3);
            prodAnalysis3.setYfzq("0");
            prodAnalysis3.setCreatTime(new Date());
            proAnalysisVo.setFeed(prodAnalysis3);
            count = 0;
        } else {
            count = Integer.parseInt(prodAnalysis.getZcb());
            proAnalysisVo.setFeed(prodAnalysis);
        }

        QueryWrapper<ProdAnalysis> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("display_type",Integer.parseInt(displayType));
        queryWrapper1.eq("farm_id", farmId).eq("type", 2).orderByDesc("creat_time")
                .last(" limit 1");
        ProdAnalysis prodAnalysis1 = prodAnalysisMapper.selectOne(queryWrapper1);
        if (ObjectUtil.isEmpty(prodAnalysis1)) {
            ProdAnalysis prodAnalysis3 = new ProdAnalysis();
            prodAnalysis3.setZcb("0");
            prodAnalysis3.setByzq("0");
            prodAnalysis3.setFarmId(Integer.parseInt(farmId));
            prodAnalysis3.setHbzq("0");
            prodAnalysis3.setId(0);
            prodAnalysis3.setJczq("0");
            prodAnalysis3.setType(3);
            prodAnalysis3.setYfzq("0");
            prodAnalysis3.setCreatTime(new Date());
            prodAnalysis3.setFeedCost("57");
            prodAnalysis3.setShareCost("36");
            prodAnalysis3.setVaccineCost("7");
            proAnalysisVo.setShare(prodAnalysis3);
            count1 = 0;
        } else {
            count1 = Integer.parseInt(prodAnalysis1.getZcb());
            proAnalysisVo.setShare(prodAnalysis1);
        }


        QueryWrapper<ProdAnalysis> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("display_type",Integer.parseInt(displayType));
        queryWrapper2.eq("farm_id", farmId).eq("type", 3).orderByDesc("creat_time")
                .last(" limit 1");
        ProdAnalysis prodAnalysis2 = prodAnalysisMapper.selectOne(queryWrapper2);
        if (ObjectUtil.isEmpty(prodAnalysis2)) {
            ProdAnalysis prodAnalysis3 = new ProdAnalysis();
            prodAnalysis3.setZcb("0");
            prodAnalysis3.setByzq("0");
            prodAnalysis3.setFarmId(Integer.parseInt(farmId));
            prodAnalysis3.setHbzq("0");
            prodAnalysis3.setId(0);
            prodAnalysis3.setJczq("0");
            prodAnalysis3.setType(3);
            prodAnalysis3.setYfzq("0");
            prodAnalysis3.setCreatTime(new Date());
            proAnalysisVo.setDrug(prodAnalysis3);
            count2 = 0;
        } else {
            count2 = Integer.parseInt(prodAnalysis2.getZcb());
            proAnalysisVo.setDrug(prodAnalysis2);
        }

        proAnalysisVo.setCount(String.valueOf(count+count1+count2));

        return new Result(ResultCode.SUCCESS,proAnalysisVo);
    }

    @Override
    public Result edit(HttpServletRequest httpServletRequest, ProdAnalysis prodAnalysis) {
        prodAnalysis.setDisplayType(1);
        QueryWrapper<ProdAnalysis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", prodAnalysis.getId());
        ProdAnalysis prodAnalysis1 = prodAnalysisMapper.selectOne(queryWrapper);
        ProdAnalysis prodAnalysis2 = new ProdAnalysis();
        if (ObjectUtil.isEmpty(prodAnalysis1)) {
            prodAnalysisMapper.insert(prodAnalysis);
            return new Result(ResultCode.SUCCESS,"添加成功");
        } else {
            prodAnalysisMapper.updateById(prodAnalysis);
            return new Result(ResultCode.SUCCESS,"修改成功");
        }

    }

}
