package vip.xiaonuo.production.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.production.entity.ProductionWritePlan;
import vip.xiaonuo.production.entity.ProductionWriteRate;
import vip.xiaonuo.production.mapper.ProductionWriteRateMapper;
import vip.xiaonuo.production.param.ProductionPageParam;
import vip.xiaonuo.production.param.ProductionWriteRateAddParam;
import vip.xiaonuo.production.service.IProductionWriteRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.mapper.SysOrgMapper;
import vip.xiaonuo.write.entity.WriteThreshold;
import vip.xiaonuo.write.entity.WriteWarning;
import vip.xiaonuo.write.mapper.WriteThresholdMapper;
import vip.xiaonuo.write.mapper.WriteWarningMapper;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产收率填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-01
 */
@Service
public class ProductionWriteRateServiceImpl extends ServiceImpl<ProductionWriteRateMapper, ProductionWriteRate> implements IProductionWriteRateService {

    @Autowired
    private ProductionWriteRateMapper writeRateMapper;
    @Autowired
    private SysOrgMapper orgMapper;
    @Autowired
    private WriteThresholdMapper thresholdMapper;
    @Autowired
    private WriteWarningMapper warningMapper;

    @Override
    public CommonResult add(ProductionWriteRateAddParam param) {
        String orgId = param.getOrgId();
        SysOrg org = orgMapper.selectOne(new QueryWrapper<SysOrg>().eq("id", orgId));
        ProductionWriteRate writeRate = new ProductionWriteRate();
        BeanUtil.copyProperties(param, writeRate);
        writeRate.setOrgName(org.getName());
        writeRateMapper.insert(writeRate);
        String rate = param.getProductionRate();
        Double value = Double.valueOf(rate);
        QueryWrapper<WriteThreshold> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id",param.getOrgId()).eq("type_s",5);
        List<WriteThreshold> thresholds = thresholdMapper.selectList(queryWrapper);
        if (thresholds.size() != 0) {
            for (WriteThreshold threshold : thresholds) {
                String valuesS = threshold.getValuesS();
                Double valueOf = Double.valueOf(valuesS);
                Integer isTrue = threshold.getIsTrue();
                if (0 == isTrue) {
                    if (value < valueOf) {
                        WriteWarning warning = new WriteWarning();
                        warning.setWarningDate(new Date());
                        warning.setWarningType(threshold.getIndexS());
                        warning.setWarningLevel(threshold.getLevelS().toString());
                        warning.setWarningContent(threshold.getIndexS() + rate + "小于阈值");
                        warning.setUserName(threshold.getUserName());
                        warning.setUserId(threshold.getUserId());
                        warning.setType(5);
                        warningMapper.insert(warning);
                    }
                } else if (1 == isTrue) {
                    WriteWarning warning = new WriteWarning();
                    warning.setWarningDate(new Date());
                    warning.setWarningType(threshold.getIndexS());
                    warning.setWarningLevel(threshold.getLevelS().toString());
                    warning.setWarningContent(threshold.getIndexS() + rate + "等于阈值");
                    warning.setUserName(threshold.getUserName());
                    warning.setUserId(threshold.getUserId());
                    warning.setType(5);
                    warningMapper.insert(warning);
                } else if (2 == isTrue) {
                    WriteWarning warning = new WriteWarning();
                    warning.setWarningDate(new Date());
                    warning.setWarningType(threshold.getIndexS());
                    warning.setWarningLevel(threshold.getLevelS().toString());
                    warning.setWarningContent(threshold.getIndexS() + rate + "大于阈值");
                    warning.setUserName(threshold.getUserName());
                    warning.setUserId(threshold.getUserId());
                    warning.setType(5);
                    warningMapper.insert(warning);
                }
            }
        }

        return CommonResult.ok();
    }

    @Override
    public CommonResult list(Map<String, String> paramsMap) {
        return null;
    }

    @Override
    public CommonResult edit(ProductionWriteRate writeRate) {
        writeRateMapper.updateById(writeRate);
        return CommonResult.ok();
    }

    @Override
    public CommonResult delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        writeRateMapper.deleteById(id);
        return CommonResult.ok();
    }

    @Override
    public Page<ProductionWriteRate> page(ProductionPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        QueryWrapper<ProductionWriteRate> queryWrapper = new QueryWrapper();
        Page<ProductionWriteRate> page = new Page<>(pageNum, pageSize);
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return writeRateMapper.selectPage(page, queryWrapper);
    }
}
