package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.exception.enums.GlobalErrorCodeConstants;
import com.gxwebsoft.common.core.utils.LoginUserUtil;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.mapper.ShopSurchargeConfigMapper;
import com.gxwebsoft.shop.service.ShopSurchargeConfigService;
import com.gxwebsoft.shop.entity.ShopSurchargeConfig;
import com.gxwebsoft.shop.param.ShopSurchargeConfigParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 步梯费用设置Service实现
 *
 * @author xm
 * @since 2026-04-28 16:30:00
 */
@Service
@AllArgsConstructor
public class ShopSurchargeConfigServiceImpl extends ServiceImpl<ShopSurchargeConfigMapper, ShopSurchargeConfig> implements ShopSurchargeConfigService {

    @Override
    public PageResult<ShopSurchargeConfig> pageRel(ShopSurchargeConfigParam param) {
        PageParam<ShopSurchargeConfig, ShopSurchargeConfigParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ShopSurchargeConfig> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopSurchargeConfig> listRel(ShopSurchargeConfigParam param) {
        List<ShopSurchargeConfig> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ShopSurchargeConfig, ShopSurchargeConfigParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopSurchargeConfig getByIdRel(Integer id) {
        ShopSurchargeConfigParam param = new ShopSurchargeConfigParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public ShopSurchargeConfig getInfoByType(Integer type) {
        List<ShopSurchargeConfig> list = lambdaQuery().eq(ShopSurchargeConfig::getType, type).list();
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Integer saveInfo(ShopSurchargeConfig entity) {
        List<ShopSurchargeConfig> list = lambdaQuery().eq(ShopSurchargeConfig::getType, entity.getType()).list();
        if(CollectionUtils.isNotEmpty(list)){
            throw new RuntimeException("该类型收费数据已存在！");
        }
        User loginUser = LoginUserUtil.getLoginUser();
        entity.setCreator(String.valueOf(loginUser.getUserId()));
        entity.setCreateTime(LocalDateTime.now());

        baseMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public Boolean updateInfo(ShopSurchargeConfig entity) {
        List<ShopSurchargeConfig> list = lambdaQuery().eq(ShopSurchargeConfig::getType, entity.getType()).list();
        if(CollectionUtils.isNotEmpty(list)){
            throw new RuntimeException("该类型收费数据已存在！");
        }

        User loginUser = LoginUserUtil.getLoginUser();
        entity.setUpdater(String.valueOf(loginUser.getUserId()));
        entity.setUpdateTime(LocalDateTime.now());

        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public Boolean updateStatus(Integer id) {
        User loginUser = LoginUserUtil.getLoginUser();
        if(loginUser == null){
            throw new RuntimeException(GlobalErrorCodeConstants.UNAUTHORIZED.getMsg());
        }

        ShopSurchargeConfig config = baseMapper.selectById(id);
        if(config == null){
            throw new RuntimeException(GlobalErrorCodeConstants.NOT_FOUND.getMsg());
        }

        if(config.getStatus() == 0){
            config.setStatus(1);
        }else {
            config.setStatus(0);
        }
        config.setUpdater(String.valueOf(loginUser.getUserId()));
        config.setUpdateTime(LocalDateTime.now());

        return baseMapper.updateById(config) > 0;
    }

}
