package com.sckw.mine.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.utils.TenantUtil;
import com.sckw.core.web.response.HttpResult;
import com.sckw.mine.common.OrderServerCommon;
import com.sckw.mine.entity.KwBusinessMineralAggregate;
import com.sckw.mine.entity.req.MineralAggPageListParam;
import com.sckw.mine.entity.req.MineralAggregateInsertParam;
import com.sckw.mine.entity.req.MineralAggregateUpdateParam;
import com.sckw.mine.entity.res.KwBusinessMineralAggregateDetailRes;
import com.sckw.mine.entity.res.MineralAggregateListRes;
import com.sckw.mine.enums.MineralAggregateTypeEnum;
import com.sckw.mine.mapper.MineralAggregateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-05-30
 */
@Service
public class MineralAggregateService {

    @Autowired
    MineralAggregateMapper mineralAggregateMapper;

    @Autowired
    OrderServerCommon orderServerCommon;

    /*
    矿料添加
     */
    public HttpResult insert(MineralAggregateInsertParam param) {

        if (param == null) {
            return HttpResult.error("必要参数缺失");
        }

        LambdaQueryWrapper<KwBusinessMineralAggregate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwBusinessMineralAggregate::getName, param.getName())
                .eq(KwBusinessMineralAggregate::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessMineralAggregate kwBusinessMineralAggregates = mineralAggregateMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(kwBusinessMineralAggregates)) {
            throw new RuntimeException("当前矿料已存在,勿重复添加");
        }

        KwBusinessMineralAggregate kwBusinessMineralAggregate = BeanUtil.copyProperties(param, KwBusinessMineralAggregate.class);
        String code = orderServerCommon.generatorNum("LCJD", 13);
        String tenant = TenantUtil.getTenant();

        kwBusinessMineralAggregate.setId(new IdWorker(1L).nextId());
        kwBusinessMineralAggregate.setTenantId(tenant);
        kwBusinessMineralAggregate.setCode(code);
        kwBusinessMineralAggregate.setStatus(1);

        mineralAggregateMapper.insert(kwBusinessMineralAggregate);
        return HttpResult.ok();
    }


    public HttpResult AggreDelete(String id) {
        System.out.println(id);
        int ret = mineralAggregateMapper.aggreDelete(id);
        return ret>0?HttpResult.ok():HttpResult.error();
    }


    public HttpResult update(MineralAggregateUpdateParam param) {

        LambdaQueryWrapper<KwBusinessMineralAggregate> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(KwBusinessMineralAggregate::getId, param.getId())
                .eq(KwBusinessMineralAggregate::getName, param.getName())
                .eq(KwBusinessMineralAggregate::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessMineralAggregate kwBusinessMineralAggregates = mineralAggregateMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(kwBusinessMineralAggregates)){
            throw new RuntimeException("当前名称已存在");
        }

        int ret = mineralAggregateMapper.aggreUpdate(param);
        return ret>0?HttpResult.ok():HttpResult.error();
    }


    public KwBusinessMineralAggregateDetailRes detail(String id) {
        KwBusinessMineralAggregateDetailRes load = mineralAggregateMapper.load(id);
        load.setStatusStr(load.getStatus()==1?"启用":"禁用");
        load.setTypeStr(MineralAggregateTypeEnum.getNameByCode(load.getType()));
        return load;
    }


    public HttpResult pageList(MineralAggPageListParam param) {

        String tenant = TenantUtil.getTenant();
        if (StringUtils.isNotBlank(tenant)) {
            param.setTenantId(tenant);
        }
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<MineralAggregateListRes> businessTruckPageListRes = mineralAggregateMapper.mineAggregatePageList(param);
        businessTruckPageListRes.forEach(e -> {
            e.setTypeStr(MineralAggregateTypeEnum.getNameByCode(e.getType()));
            e.setStatusStr(e.getStatus()==1?"启用":"禁用");
            e.setUnitStr(e.getUnit()==1?"吨":"件");
        });
        return HttpResult.ok(new PageRes<>(new PageInfo<>(businessTruckPageListRes)));
    }

}
