package shop.alien.store.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.StorePersonnel;
import shop.alien.entity.store.vo.StorePersonnelVo;
import shop.alien.mapper.StorePersonnelMapper;
import shop.alien.store.service.StoreImgService;
import shop.alien.store.service.StorePersonnelService;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺人员服务实现类
 *
 * @author system
 * @since 2025-01-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StorePersonnelServiceImpl extends ServiceImpl<StorePersonnelMapper, StorePersonnel> implements StorePersonnelService {

    private final StorePersonnelMapper storePersonnelMapper;

    private final StoreImgService storeImgService;

    /**
     * 获取店铺人员列表
     *
     * @param storeId 门店id
     * @return List<StorePersonnelVo>
     */
    @Override
    public List<StorePersonnelVo> getStorePersonnelList(Integer storeId) {
        List<StorePersonnelVo> personnelList = storePersonnelMapper.getStorePersonnelList(storeId);
        // 按排序字段排序
        return personnelList.stream()
                .sorted(Comparator.comparing(StorePersonnelVo::getSort, Comparator.nullsLast(Integer::compareTo)))
                .collect(Collectors.toList());
    }

    /**
     * 获取人员详情
     *
     * @param id 人员id
     * @return StorePersonnelVo
     */
    @Override
    public StorePersonnelVo getPersonnelInfo(Integer id) {
        return storePersonnelMapper.getPersonnelInfo(id);
    }

    /**
     * 新增或修改店铺人员
     *
     * @param storePersonnelVo 人员信息
     * @return R<String>
     */
    @Override
    public R<String> saveOrUpdatePersonnel(StorePersonnelVo storePersonnelVo) {
        boolean flag = false;
        LambdaQueryWrapper<StorePersonnel> queryWrapper = new LambdaQueryWrapper<>();
        Integer imgId = 0;

        // 处理图片信息
        if (storePersonnelVo.getImgId() == null || storePersonnelVo.getImgId() == 0) {
            if (StringUtils.isNotEmpty(storePersonnelVo.getImgUrl())) {
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storePersonnelVo.getStoreId());
                // 图片类型：12-人员头像（根据实际业务调整）
                storeImg.setImgType(12);
                storeImg.setImgUrl(storePersonnelVo.getImgUrl());
                storeImg.setImgDescription(storePersonnelVo.getPersonnelName());
                storeImgService.saveOrUpdate(storeImg);
                imgId = storeImg.getId();
            }
        } else {
            imgId = storePersonnelVo.getImgId();
        }

        // 封装StorePersonnel参数
        StorePersonnel storePersonnel = new StorePersonnel();
        BeanUtils.copyProperties(storePersonnelVo, storePersonnel);
        storePersonnel.setImgId(imgId);

        // 修改人员
        if (storePersonnel.getId() != null) {
            flag = this.updateById(storePersonnel);
            if (!flag) {
                log.error("人员修改失败");
                return R.fail("人员修改失败");
            }
            return R.success("人员修改成功");
        } else {
            // 新增人员
            // 校验人员参数
            if (StringUtils.isEmpty(storePersonnel.getPersonnelName())) {
                return R.fail("请输入人员姓名");
            }

            // 计算排序值
            queryWrapper.eq(StorePersonnel::getStoreId, storePersonnel.getStoreId());
            List<StorePersonnel> personnelList = this.list(queryWrapper);
            if (CollectionUtil.isNotEmpty(personnelList)) {
                Integer maxSort = personnelList.stream()
                        .map(StorePersonnel::getSort)
                        .filter(sort -> sort != null)
                        .max(Integer::compareTo)
                        .orElse(0);
                storePersonnel.setSort(maxSort + 1);
            } else {
                storePersonnel.setSort(1);
            }

            // 保存人员
            flag = this.save(storePersonnel);
            if (!flag) {
                return R.fail("人员新增失败");
            }
        }
        return R.success("新增人员成功");
    }

    /**
     * 删除店铺人员
     *
     * @param ids 人员id列表
     * @return R<String>
     */
    @Override
    public R<String> deletePersonnel(List<Integer> ids) {
        boolean flag = this.removeByIds(ids);
        if (!flag) {
            return R.fail("删除失败");
        }
        return R.success("删除成功");
    }

    /**
     * 保存人员排序
     *
     * @param storePersonnelList 人员列表
     * @return Boolean
     */
    @Override
    public Boolean savePersonnelSort(List<StorePersonnel> storePersonnelList) {
        return this.updateBatchById(storePersonnelList);
    }
}

