package com.younike.system.service.impl;

import com.younike.common.core.domain.model.LoginUser;
import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.id.IndexGenerate;
import com.younike.system.domain.Agent;
import com.younike.system.mapper.AgentMapper;
import com.younike.system.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 商户代理商Service业务层处理
 * 
 * @author dail
 * @date 2022-03-09
 */
@Service
public class AgentServiceImpl implements IAgentService {
    @Autowired
    private AgentMapper agentMapper;

    /**
     * 查询商户代理商
     * 
     * @param agentId 商户代理商主键
     * @return 商户代理商
     */
    @Override
    public Agent selectAgentByAgentId(Long agentId) {
        return agentMapper.selectAgentByAgentId(agentId);
    }

    /**
     * 查询商户代理商列表
     * 
     * @param agent 商户代理商
     * @return 商户代理商
     */
    @Override
    public List<Agent> selectAgentList(Agent agent) {
        return agentMapper.selectAgentList(agent);
    }

    /**
     * 新增商户代理商
     * 
     * @param agent 商户代理商
     * @return 结果
     */
    @Override
    public int insertAgent(Agent agent) {
        // 获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 生成代理商编号
        agent.setAgentIndex(IndexGenerate.getAgentIndex());
        agent.setCreateBy(loginUser == null ? "1" : String.valueOf(loginUser.getUserId()));
        agent.setUpdateBy(loginUser == null ? "1" : String.valueOf(loginUser.getUserId()));
        return agentMapper.insertAgent(agent);
    }

    /**
     * 修改商户代理商
     * 
     * @param agent 商户代理商
     * @return 结果
     */
    @Override
    public int updateAgent(Agent agent) {
        agent.setUpdateTime(LocalDateTime.now());
        return agentMapper.updateAgent(agent);
    }

    /**
     * 批量删除商户代理商
     * 
     * @param agentIds 需要删除的商户代理商主键
     * @return 结果
     */
    @Override
    public int deleteAgentByAgentIds(Long[] agentIds) {
        return agentMapper.deleteAgentByAgentIds(agentIds);
    }

    /**
     * 删除商户代理商信息
     * 
     * @param agentId 商户代理商主键
     * @return 结果
     */
    @Override
    public int deleteAgentByAgentId(Long agentId)
    {
        return agentMapper.deleteAgentByAgentId(agentId);
    }

    /**
     * 逻辑删除商户代理商信息
     *
     * @param agentId
     * @return
     */
    @Override
    public int logicalRemoveById(Long agentId) {
        Agent agent = new Agent();
        agent.setAgentId(agentId);
        agent.setDelFlag("2");
        return agentMapper.updateAgent(agent);
    }
}
