package com.heketai.website.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.common.result.ReplacePictureRequest;
import com.heketai.website.entity.dto.ReplacePictureDto;
import com.heketai.website.entity.Product;
import com.heketai.website.mapper.ProductMapper;
import com.heketai.website.service.ReplacePictureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品图片，按照封装来操作
 * yanBin
 * 2021/9/23
 */
@Service
public class ReplacePictureServiceImpl extends ServiceImpl<ProductMapper, Product> implements ReplacePictureService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public BaseResult selectMyPage(ReplacePictureRequest request) {
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 10 : request.getSize();
        Integer pages = (page-1)*size<0 ? 0 : (page-1)*size;
        request.setPage(pages);
        request.setSize(size);
        List<ReplacePictureDto> replacePictureDtoList = productMapper.selectMyPage(request);
        int count = productMapper.selectMyCount(request);
        Map map = new HashMap();
        map.put("replacePictureDtoList",replacePictureDtoList);
        map.put("count",count);
        return BaseResult.successWithData(map);
    }

    @Override
    public BaseResult updateMyImg(ReplacePictureDto request) {
        if (StringUtils.isBlank(request.getImg())){
            return BaseResult.fail("201","失败，图片路径为空");
        }
        if (StringUtils.isBlank(request.getPackageSpecification())){
            return BaseResult.fail("201","失败，封装类型为空");
        }
        Boolean isUpdate = productMapper.updateMyImg(request);
        if(!isUpdate){
            return BaseResult.fail("201","失败,更新数据库失败");
        }
        return BaseResult.fail("200","操作成功");
    }
}
