package com.seer.teach.mall.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.mall.entity.SpecificationEntity;
import com.seer.teach.mall.request.SpecificationPageReq;
import com.seer.teach.mall.request.SpecificationSaveReq;
import com.seer.teach.mall.request.SpecificationUpdateReq;
import com.seer.teach.mall.response.SpecificationPageResp;
import com.seer.teach.mall.response.SpecificationResp;
import com.seer.teach.mall.mapper.SpecificationMapper;
import com.seer.teach.mall.good.service.IAdminSpecificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 规格表 服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@Service
@Slf4j
public class AdminSpecificationServiceImpl extends ServiceImpl<SpecificationMapper, SpecificationEntity> implements IAdminSpecificationService {

    /**
     * 商品规格分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageListBean<SpecificationPageResp> getSpecificationPage(SpecificationPageReq params) {
        Page<SpecificationEntity> page = new Page<>(params.getPage(), params.getLimits());
        Page<SpecificationEntity> resultPage = this.page(page,
                Wrappers.lambdaQuery(SpecificationEntity.class)
                        .like(StringUtils.isNotBlank(params.getName()),
                                SpecificationEntity::getName,params.getName().trim())
                        .orderByAsc(SpecificationEntity::getSortOrder)
        );
        List<SpecificationPageResp> respList = resultPage.getRecords().stream().map(entity -> {
            SpecificationPageResp resp = new SpecificationPageResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        }).collect(Collectors.toList());
        PageListBean<SpecificationPageResp> pageList = new PageListBean<>();
        pageList.setList(respList);
        pageList.setPage(resultPage.getCurrent());
        pageList.setPageSize(resultPage.getSize());
        pageList.setTotal(resultPage.getTotal());
        pageList.setTotalPage(resultPage.getPages());
        return pageList;
    }

    /**
     * 新增商品规格
     *
     * @param params
     * @return
     */
    @Override
    public void saveSpecification(SpecificationSaveReq params) {
        SpecificationEntity one = getOne(new LambdaQueryWrapper<SpecificationEntity>()
                .eq(SpecificationEntity::getName, params.getName()));
        AssertUtils.isNull(one,ResultCodeEnum.GOOD_SPECIFICATIONS_ALREADY_EXIST);
        SpecificationEntity specificationEntity = new SpecificationEntity();
        BeanUtils.copyProperties(params,specificationEntity);
        save(specificationEntity);
    }

    /**
     * 根据id查询商品规格信息
     *
     * @param id
     * @return
     */
    @Override
    public SpecificationResp getSpecificationInfo(Integer id) {
        SpecificationEntity specificationEntity = getById(id);
        if(specificationEntity != null){
            SpecificationResp specificationResp = new SpecificationResp();
            BeanUtils.copyProperties(specificationEntity,specificationResp);
            return specificationResp;
        }
        throw new CommonException(ResultCodeEnum.NO_GOODS_SPECIFICATION_INFO);
    }

    /**
     * 修改商品规格信息
     *
     * @param params
     * @return
     */
    @Override
    public void updateSpcification(SpecificationUpdateReq params) {
        SpecificationEntity result = getById(params.getId());
        AssertUtils.notNull(result,ResultCodeEnum.GOOD_SPECIFICATIONS_NOT_EXIST);
        SpecificationEntity one = getOne(new LambdaQueryWrapper<SpecificationEntity>()
                .eq(SpecificationEntity::getName, params.getName()));
        AssertUtils.isNull(one,ResultCodeEnum.GOOD_SPECIFICATIONS_ALREADY_EXIST);
        result.setName(params.getName());
        result.setSortOrder(params.getSortOrder());
        updateById(result);
    }

    /**
     * 删除商品规格信息
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteSpecification(List<Integer> ids) {
        removeByIds(ids);
    }

    /**
     * 查看所有商品规格列表
     *
     * @return
     */
    @Override
    public List<SpecificationResp> listSpecification() {
        List<SpecificationEntity> list = list(new LambdaQueryWrapper<SpecificationEntity>()
                .eq(SpecificationEntity::getDeleted, 0)
                .orderByAsc(SpecificationEntity::getSortOrder));
        List<SpecificationResp> resultList = new ArrayList<>();
        list.forEach(i->{
            SpecificationResp result = new SpecificationResp();
            BeanUtils.copyProperties(i,result);
            resultList.add(result);
        });
        return resultList;
    }

}
