package com.cool.store.service.impl;

import com.cool.store.dao.PreFriedProductsDAO;
import com.cool.store.dto.pre.fry.PreFriedProductsBatchDTO;
import com.cool.store.dto.pre.fry.PreFriedProductsDTO;
import com.cool.store.dto.pre.fry.PreFriedProductsDetailDTO;
import com.cool.store.dto.pre.fry.PreFriedProductsQueryDTO;
import com.cool.store.entity.PreFriedProductsDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.PreFriedProductsMapper;
import com.cool.store.service.PreFriedProductsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/6/20 19:47
 * @Version 1.0
 */
@Service
public class PreFriedProductsServiceImpl  implements PreFriedProductsService {
    @Resource
    private PreFriedProductsDAO preFriedProductsDAO;

    @Override
    public Long createProduct(PreFriedProductsDTO dto, String userId) {
        if (dto==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        PreFriedProductsDO preFriedProductsDO = preFriedProductsDAO.queryByProductCode(dto.getProductCode());
        if (preFriedProductsDO!=null){
            throw  new ServiceException(ErrorCodeEnum.PRODUCTS_CODE_EXIST);
        }
        PreFriedProductsDO product = convertToDO(dto);
        product.setCreatedTime(new Date());
        product.setCreatedUserId(userId);
        preFriedProductsDAO.createProduct(product);
        return product.getId();
    }

    @Override
    public Long updateProduct(PreFriedProductsDTO dto, String userId) {
        if (dto==null||dto.getId() == null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        PreFriedProductsDO preFriedProductsDO = preFriedProductsDAO.queryByProductCode(dto.getProductCode());
        if (preFriedProductsDO!=null && !preFriedProductsDO.getId().equals(dto.getId())){
            throw  new ServiceException(ErrorCodeEnum.PRODUCTS_CODE_EXIST);
        }
        PreFriedProductsDO product = convertToDO(dto);
        product.setUpdatedTime(new Date());
        product.setUpdatedUserId(userId);
        preFriedProductsDAO.updateProduct( product);
        return product.getId();
    }

    @Override
    public int batchDelete(PreFriedProductsBatchDTO batchDTO, String userId) {
        validateBatchOperation(batchDTO);
        return preFriedProductsDAO.batchDeleteProducts(batchDTO.getIds(), userId);
    }

    @Override
    public int batchUpdateStatus(PreFriedProductsBatchDTO batchDTO, String userId) {
        validateBatchOperation(batchDTO);
        if (batchDTO.getStatus() == null || (batchDTO.getStatus() != 0 && batchDTO.getStatus() != 1)) {
            throw new IllegalArgumentException("状态值必须为0或1");
        }
        return preFriedProductsDAO.batchUpdateProductStatus(
                batchDTO.getIds(),
                batchDTO.getStatus(),
                userId
        );
    }

    @Override
    public PageInfo<PreFriedProductsDetailDTO> queryProducts(PreFriedProductsQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(),queryDTO.getPageSize());
        List<PreFriedProductsDO> dos = preFriedProductsDAO.selectByCondition(
                queryDTO.getProductCode(),
                queryDTO.getProductName(),
                queryDTO.getStatus()
        );
        PageInfo preFriedProductsDOPageInfo = new PageInfo<>(dos);
        List<PreFriedProductsDetailDTO> preFriedProductsDTOS = convertToDTOs(dos);
        preFriedProductsDOPageInfo.setList(preFriedProductsDTOS);
        return preFriedProductsDOPageInfo;
    }

    @Override
    public PreFriedProductsDetailDTO queryById(Long id) {
        //查询详情
        PreFriedProductsDO product = preFriedProductsDAO.queryById(id);
        if (product == null){
            throw new ServiceException(ErrorCodeEnum.PRE_FRY_PRODUCT_NOT_EXIST);
        }
        return convertToDTO(product);
    }


    /**
     * 转换方法
     * @param dto
     * @return
     */
    private PreFriedProductsDO convertToDO(PreFriedProductsDTO dto) {
        PreFriedProductsDO product = new PreFriedProductsDO();
        BeanUtils.copyProperties(dto, product);
        return product;
    }

    private List<PreFriedProductsDetailDTO> convertToDTOs(List<PreFriedProductsDO> dos) {
        return dos.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    private PreFriedProductsDetailDTO convertToDTO(PreFriedProductsDO product) {
        if (product != null) {
            PreFriedProductsDetailDTO dto = new PreFriedProductsDetailDTO();
            BeanUtils.copyProperties(product, dto);
            return dto;
        } else {
            return null;
        }
    }

    private void validateBatchOperation(PreFriedProductsBatchDTO batchDTO) {
        if (batchDTO.getIds() == null || batchDTO.getIds().isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }
    }


}
