package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.system.service.AreaService;
import com.gxwebsoft.shop.mapper.CommissionRoleMapper;
import com.gxwebsoft.shop.service.CommissionRoleService;
import com.gxwebsoft.shop.entity.CommissionRole;
import com.gxwebsoft.shop.param.CommissionRoleParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分红角色Service实现
 *
 * @author 科技小王子
 * @since 2024-12-02 12:43:40
 */
@Service
public class CommissionRoleServiceImpl extends ServiceImpl<CommissionRoleMapper, CommissionRole> implements CommissionRoleService {
    @Resource
    private AreaService areaService;

    @Override
    public PageResult<CommissionRole> pageRel(CommissionRoleParam param) {
        PageParam<CommissionRole, CommissionRoleParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<CommissionRole> list = baseMapper.selectPageRel(page, param);
        for (CommissionRole commissionRole : list) {
            if (commissionRole.getProvinceId() != null) {
                commissionRole.setProvince(areaService.getByIdRel(commissionRole.getProvinceId()));
            }
            if (commissionRole.getCityId() != null) {
                commissionRole.setCity(areaService.getByIdRel(commissionRole.getCityId()));
            }
            if (commissionRole.getRegionId() != null) {
                commissionRole.setRegion(areaService.getByIdRel(commissionRole.getRegionId()));
            }
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<CommissionRole> listRel(CommissionRoleParam param) {
        List<CommissionRole> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<CommissionRole, CommissionRoleParam> page = new PageParam<>();
        return page.sortRecords(list);
    }

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

    @Override
    public List<CommissionRole> listByProvinceId(Integer provinceId) {
        return list(
                new LambdaQueryWrapper<CommissionRole>()
                        .eq(CommissionRole::getProvinceId, provinceId)
        );
    }

}
