package shop.alien.storeplatform.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.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.storeplatform.entity.StorePlatformStoreStaffConfig;
import shop.alien.storeplatform.mapper.StorePlatformStoreStaffConfigMapper;
import shop.alien.storeplatform.service.StorePlatformStoreStaffConfigService;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class StorePlatformStoreStaffConfigServiceImpl extends ServiceImpl<StorePlatformStoreStaffConfigMapper, StorePlatformStoreStaffConfig> implements StorePlatformStoreStaffConfigService {

    private final StorePlatformStoreStaffConfigMapper staffConfigMapper;

    @Override
    public StorePlatformStoreStaffConfig getById(Integer id) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.getById id={}", id);
        if (id == null) {
            throw new RuntimeException("员工ID不能为空");
        }
        return super.getById(id);
    }

    @Override
    public List<StorePlatformStoreStaffConfig> getListByStoreId(Integer storeId) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.getListByStoreId storeId={}", storeId);
        if (storeId == null) {
            throw new RuntimeException("门店ID不能为空");
        }
        LambdaQueryWrapper<StorePlatformStoreStaffConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StorePlatformStoreStaffConfig::getStoreId, storeId)
                .eq(StorePlatformStoreStaffConfig::getDeleteFlag, 0)
                .orderByDesc(StorePlatformStoreStaffConfig::getTopStatus)
                .orderByAsc(StorePlatformStoreStaffConfig::getId);
        return this.list(queryWrapper);
    }

    @Override
    public R<String> saveStaff(StorePlatformStoreStaffConfig staffConfig) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.saveStaff staffConfig={}", staffConfig);
        String validateResult = validateStaffConfig(staffConfig, false);
        if (validateResult != null) {
            return R.fail(validateResult);
        }

        boolean flag = this.save(staffConfig);
        if (!flag) {
            return R.fail("新增员工失败");
        }
        return R.success("新增员工成功");
    }

    @Override
    public R<String> updateStaff(StorePlatformStoreStaffConfig staffConfig) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.updateStaff staffConfig={}", staffConfig);
        if (staffConfig == null || staffConfig.getId() == null) {
            return R.fail("员工信息或员工ID不能为空");
        }
        String validateResult = validateStaffConfig(staffConfig, true);
        if (validateResult != null) {
            return R.fail(validateResult);
        }

        boolean flag = this.updateById(staffConfig);
        if (!flag) {
            return R.fail("修改员工失败");
        }
        return R.success("修改员工成功");
    }

    @Override
    public R<String> saveOrUpdateStaff(StorePlatformStoreStaffConfig staffConfig) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.saveOrUpdateStaff staffConfig={}", staffConfig);
        if (staffConfig == null) {
            return R.fail("员工信息不能为空");
        }
        if (staffConfig.getId() == null) {
            return saveStaff(staffConfig);
        } else {
            return updateStaff(staffConfig);
        }
    }

    @Override
    public R<String> deleteStaff(Integer id) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.deleteStaff id={}", id);
        if (id == null) {
            return R.fail("员工ID不能为空");
        }
        boolean flag = this.removeById(id);
        if (!flag) {
            return R.fail("删除员工失败");
        }
        return R.success("删除员工成功");
    }

    @Override
    public R<String> deleteStaffBatch(List<Integer> ids) {
        log.info("StorePlatformStoreStaffConfigServiceImpl.deleteStaffBatch ids={}", ids);
        if (CollectionUtil.isEmpty(ids)) {
            return R.fail("员工ID列表不能为空");
        }
        boolean flag = this.removeByIds(ids);
        if (!flag) {
            return R.fail("批量删除员工失败");
        }
        return R.success("批量删除员工成功");
    }

    /**
     * 校验员工数据
     */
    private String validateStaffConfig(StorePlatformStoreStaffConfig staffConfig, boolean isUpdate) {
        if (staffConfig == null) {
            return "员工信息不能为空";
        }
        if (!isUpdate && staffConfig.getStoreId() == null) {
            return "门店ID不能为空";
        }
        if (StringUtils.isEmpty(staffConfig.getName())) {
            return "员工姓名不能为空";
        }
        if (StringUtils.isEmpty(staffConfig.getStaffPosition())) {
            return "员工职位不能为空";
        }
        if (StringUtils.isEmpty(staffConfig.getStatus())) {
            staffConfig.setStatus("0");
        }
        if (staffConfig.getTopStatus() == null) {
            staffConfig.setTopStatus(0);
        }
        return null;
    }
}
