package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.exception.AgentExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.AgentBo;
import com.ruoyi.clock.domain.vo.AgentVo;
import com.ruoyi.clock.domain.Agent;
import com.ruoyi.clock.mapper.AgentMapper;
import com.ruoyi.clock.service.IAgentService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 代理商信息Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class AgentServiceImpl implements IAgentService {

    private final AgentMapper baseMapper;

    /**
     * 查询代理商信息分页
     *
     * @param bo 代理商信息
     * @return 代理商信息
     */
    @Override
    public TableDataInfo<AgentVo> queryPageList(AgentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Agent> lqw = buildQueryWrapper(bo);
        Page<AgentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询代理商信息列表
     *
     * @param bo 代理商信息
     * @return 代理商信息
     */
    @Override
    public List<AgentVo> queryList(AgentBo bo) {
        LambdaQueryWrapper<Agent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Agent> buildQueryWrapper(AgentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Agent> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), Agent::getAgentName, bo.getAgentName());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceArea()), Agent::getServiceArea, bo.getServiceArea());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Agent::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getContact()), Agent::getContact, bo.getContact());
        lqw.like(StringUtils.isNotBlank(bo.getPhonenumber()), Agent::getPhonenumber, bo.getPhonenumber());
        lqw.orderByDesc(Agent::getUpdateTime);
        return lqw;
    }

    /**
     * 查询代理商信息
     *
     * @param agentId 代理商信息主键
     * @return 代理商信息
     */
    @Override
    public AgentVo queryById(Long agentId){
        return baseMapper.selectVoById(agentId);
    }

    /**
     * 详情代理商信息
     *
     * @param agentId 代理商信息主键
     * @return 代理商信息
     */
    @Override
    public Agent loadById(Long agentId, Boolean tw){
        Agent info = this.baseMapper.selectById(agentId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(AgentExceptionEnum.AGENT_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增代理商信息
     *
     * @param bo 代理商信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(AgentBo bo) {
        validEntityBeforeSave(bo);
        Agent add = BeanUtil.toBean(bo, Agent.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAgentId(add.getAgentId());
        }
        return flag;
    }

    /**
     * 修改代理商信息
     *
     * @param bo 代理商信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(AgentBo bo) {
        validEntityBeforeSave(bo);
        Agent agent = baseMapper.selectById(bo.getAgentId());
        Agent update = BeanCopyUtils.copy(bo, agent);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityBeforeSave(AgentBo bo){
        String agentName = bo.getAgentName();
        //TODO 做一些数据校验,如唯一约束
        Agent agent = getAgentByName(agentName);
        if(ObjectUtil.isNotNull(agent)&& !agent.getAgentId().equals(bo.getAgentId())){
            throw new ServiceException(AgentExceptionEnum.AGENT_REPEAT);
        }
    }

    public Agent getAgentByName(String agentName) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Agent>().eq(Agent::getAgentName,agentName)
            .last("limit 1"));
    }

    /**
     * 批量删除代理商信息
     *
     * @param ids 需要删除的代理商信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
