package vip.xiaonuo.production.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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.production.entity.ProductionEnergyPrice;
import vip.xiaonuo.production.mapper.ProductionEnergyPriceMapper;
import vip.xiaonuo.production.param.ProductionPageParam;
import vip.xiaonuo.production.service.IProductionEnergyPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 能耗单价表填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-01
 */
@Service
public class ProductionEnergyPriceServiceImpl extends ServiceImpl<ProductionEnergyPriceMapper, ProductionEnergyPrice> implements IProductionEnergyPriceService {

    @Autowired
    private ProductionEnergyPriceMapper priceMapper;

    @Override
    public CommonResult addAndEdit(ProductionEnergyPrice energyPrice) {
        String orgId = energyPrice.getOrgId();
        Date createTime = energyPrice.getCreateTime();
        ProductionEnergyPrice productionEnergyPrice = priceMapper.selectOne(new QueryWrapper<ProductionEnergyPrice>()
                .eq("org_id", orgId).eq("create_time", createTime));
        if (ObjectUtil.isEmpty(productionEnergyPrice)) {
            priceMapper.insert(energyPrice);
        } else {
            return CommonResult.error();
        }
        return CommonResult.ok();
    }

    @Override
    public CommonResult update(ProductionEnergyPrice energyPrice) {
        String orgId = energyPrice.getOrgId();
        if ("".equals(orgId) || null == orgId) {
            return CommonResult.error();
        } else {
            priceMapper.updateById(energyPrice);
        }
        return CommonResult.ok();
    }

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

    @Override
    public Page<ProductionEnergyPrice> page(ProductionPageParam pageParam) {
        String orgId = pageParam.getOrgId();
        Page<ProductionEnergyPrice> page = new Page<>();
        QueryWrapper<ProductionEnergyPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return priceMapper.selectPage(page, queryWrapper);
    }
}
