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.ProductionWriteEnergy;
import vip.xiaonuo.production.mapper.ProductionWriteEnergyMapper;
import vip.xiaonuo.production.param.ProductionWriteEnergyAddParam;
import vip.xiaonuo.production.service.IProductionWriteEnergyService;
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 java.util.Map;

/**
 * <p>
 * 车间能耗数据填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-07
 */
@Service
public class ProductionWriteEnergyServiceImpl extends ServiceImpl<ProductionWriteEnergyMapper, ProductionWriteEnergy> implements IProductionWriteEnergyService {

    @Autowired
    private ProductionWriteEnergyMapper energyMapper;
    @Autowired
    private SysOrgMapper orgMapper;
    @Override
    public CommonResult add(ProductionWriteEnergyAddParam addParam) {
        String orgId = addParam.getOrgId();
        if ("".equals(orgId) || null == orgId) {
            return CommonResult.error("未选择车间所属公司");
        }
        SysOrg sysOrg = orgMapper.selectById(addParam.getOrgId());
        ProductionWriteEnergy energy = new ProductionWriteEnergy();
        BeanUtil.copyProperties(addParam, energy);
        energy.setOrgName(sysOrg.getName());
        energyMapper.insert(energy);
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(ProductionWriteEnergy writeEnergy) {
        energyMapper.updateById(writeEnergy);
        return CommonResult.ok();
    }

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

    @Override
    public CommonResult list(Map<String, String> paramsMap) {
        String orgId = paramsMap.get("orgId");
        QueryWrapper<ProductionWriteEnergy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
        return CommonResult.data(energyMapper.selectList( queryWrapper));
    }

    @Override
    public CommonResult page(Map<String, String> paramsMap) {
        String orgId = paramsMap.get("orgId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
            pageSize = "20";
        }
        Page<ProductionWriteEnergy> page = new Page(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        QueryWrapper<ProductionWriteEnergy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
        return CommonResult.data(energyMapper.selectPage(page, queryWrapper));
    }
}
