package com.ruoyi.clock.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.Shop;
import com.ruoyi.clock.domain.bo.EmployeePasswordBo;
import com.ruoyi.clock.domain.bo.EmployeeStatusBo;
import com.ruoyi.clock.exception.EmployeeExceptionEnum;
import com.ruoyi.clock.exception.ShopExceptionEnum;
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 com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.EmployeeBo;
import com.ruoyi.clock.domain.vo.EmployeeVo;
import com.ruoyi.clock.domain.Employee;
import com.ruoyi.clock.mapper.EmployeeMapper;
import com.ruoyi.clock.service.IEmployeeService;

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

/**
 * 店员信息Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private final EmployeeMapper baseMapper;
    private final ISysConfigService sysConfigService;

    /**
     * 查询店员信息分页
     *
     * @param bo 店员信息
     * @return 店员信息
     */
    @Override
    public TableDataInfo<EmployeeVo> queryPageList(EmployeeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Employee> lqw = buildQueryWrapper(bo);
        Page<EmployeeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店员信息列表
     *
     * @param bo 店员信息
     * @return 店员信息
     */
    @Override
    public List<EmployeeVo> queryList(EmployeeBo bo) {
        LambdaQueryWrapper<Employee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Employee> buildQueryWrapper(EmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Employee> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEmployeeName()), Employee::getEmployeeName, bo.getEmployeeName());
        lqw.eq(bo.getAgentId() != null, Employee::getAgentId, bo.getAgentId());
        lqw.eq(bo.getShopId() != null, Employee::getShopId, bo.getShopId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), Employee::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Employee::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), Employee::getPassword, bo.getPassword());
        return lqw;
    }

    /**
     * 查询店员信息
     *
     * @param employeeId 店员信息主键
     * @return 店员信息
     */
    @Override
    public EmployeeVo queryById(Long employeeId){
        return baseMapper.selectVoById(employeeId);
    }

    @Override
    public Employee selectUserByUserName(String userName) {
        return baseMapper.selectUserByUserName(userName);
    }

    /**
     * 详情店员信息
     *
     * @param employeeId 店员信息主键
     * @return 店员信息
     */
    @Override
    public Employee loadById(Long employeeId, Boolean tw){
        Employee info = this.baseMapper.selectById(employeeId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(EmployeeExceptionEnum.EMPLOYEE_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增店员信息
     *
     * @param bo 店员信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(EmployeeBo bo) {
        validEntityBeforeSave(bo);
        Employee add = BeanUtil.toBean(bo, Employee.class);
        String initPassword = sysConfigService.selectConfigByKey("sys.user.initPassword");
        add.setPassword(BCrypt.hashpw(initPassword));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEmployeeId(add.getEmployeeId());
        }
        return flag;
    }

    /**
     * 修改店员信息
     * @param bo 店员信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(EmployeeBo bo) {
        validEntityBeforeSave(bo);
        Employee employee = baseMapper.selectById(bo.getEmployeeId());
        Employee update = BeanCopyUtils.copy(bo, employee);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean resetPwd(Long employeeId) {
        Employee employee = baseMapper.selectById(employeeId);
        String initPassword = sysConfigService.selectConfigByKey("sys.user.initPassword");
        employee.setPassword(BCrypt.hashpw(initPassword));
        return baseMapper.updateById(employee) > 0;
    }

    @Override
    public Boolean updatePwd(EmployeePasswordBo bo) {
        Employee employee = baseMapper.selectById(bo.getEmployeeId());
        employee.setPassword(BCrypt.hashpw(bo.getPassword()));
        return baseMapper.updateById(employee) > 0;
    }

    @Override
    public Boolean updateStatus(EmployeeStatusBo bo) {
        Employee employee = baseMapper.selectById(bo.getEmployeeId());
        employee.setStatus(bo.getStatus());
        return baseMapper.updateById(employee) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityBeforeSave(EmployeeBo bo){
        //TODO 做一些数据校验,如唯一约束
        Employee employee = getEmployeeByPhone(bo.getPhonenumber());
        if(ObjectUtil.isNotNull(employee)&& !employee.getEmployeeId().equals(bo.getEmployeeId())){
            throw new ServiceException(EmployeeExceptionEnum.EMPLOYEE_PHONENUMBER_REPEAT);
        }
    }

    public Employee getEmployeeByPhone(String phonenumber) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Employee>().eq(Employee::getPhonenumber,phonenumber)
            .last("limit 1"));
    }


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

}
