package com.xkrs.service.impl;

import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.BusinessDao;
import com.xkrs.dao.ProductContentPhotoDao;
import com.xkrs.dao.ProductCoverDao;
import com.xkrs.dao.ProductDao;
import com.xkrs.model.entity.BusinessEntity;
import com.xkrs.model.entity.ProductContentPhotoEntity;
import com.xkrs.model.entity.ProductCoverEntity;
import com.xkrs.model.entity.ProductEntity;
import com.xkrs.model.qo.ProductQo;
import com.xkrs.service.ProductService;
import com.xkrs.utils.DateTimeUtil;
import com.xkrs.utils.Query;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;

/**
 * @Author: XinYi Song
 * @Date: 2021/12/15 15:38
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductDao productDao;

    @Resource
    private BusinessDao businessDao;

    @Resource
    private ProductCoverDao productCoverDao;

    @Resource
    private ProductContentPhotoDao productContentPhotoDao;

    @Resource
    private Query query;

    /**
     * 添加商品信息
     * @param productQo      商品信息
     * @param business       商家用户id
     * @return
     */
    @Override
    public String addingProduct(ProductQo productQo, Integer business, String userIdentity) {
        Locale locale = LocaleContextHolder.getLocale();
        // 商品封面图片
        List<String> productCover = productQo.getProductCover();
        // 商品内容图片
        List<String> productContent = productQo.getProductContent();
        /*BusinessEntity byBusinessId = businessDao.findByBusinessIdAndBusinessType(business,"1");
        if(byBusinessId == null){
            return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"您还未入驻，请先入驻！",locale);
        }*/
        ProductEntity byProductName = productDao.findByProductName(productQo.getProductName());
        if(byProductName != null){
            return outputEncapsulationObject(PromptMessageEnum.FILE_EXISTS,"您已添加该商品，请勿重复添加！",locale);
        }
        if(productCover == null || productCover.size() == 0 || productContent == null || productContent.size() == 0){
            return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"商品封面或商品内容图片不能未空！",locale);
        }
        if(productCover.size() > 6 || productContent.size() > 6){
            return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"上传的图片不能超过六张！",locale);
        }
        ProductEntity productEntity = new ProductEntity();
        productEntity.setProductName(productQo.getProductName());
        productEntity.setProductDescription(productQo.getProductDescription());
        productEntity.setProductCategories(productQo.getProductCategories());
        productEntity.setProductPrice(productQo.getProductPrice());
        productEntity.setProductPro(productQo.getProductPro());
        productEntity.setProductCity(productQo.getProductCity());
        productEntity.setProductCounty(productQo.getProductCounty());
        productEntity.setBusinessId(business);
        productEntity.setShelfType("0");
        productEntity.setUserIdentity(userIdentity);
        if("affiliate_merchant".equals(userIdentity)){
            productEntity.setReviewType("0");
        }else if("county".equals(userIdentity)){
            productEntity.setReviewType("1");
        }else if("city".equals(userIdentity)){
            productEntity.setReviewType("2");
        }else {
            productEntity.setReviewType("0");
        }

        productEntity.setSubmitTime(DateTimeUtil.dateTimeToString(LocalDateTime.now()));

        productEntity.setProductCover(productCover.toString());
        productEntity.setProductContent(productContent.toString());

        productDao.save(productEntity);

        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"商品添加成功！",locale);
    }

    /**\
     * 商家查看商品上下架的信息
     * @param businessId
     * @param productCounty
     * @param shelfType
     * @return
     */
    @Override
    public String selectProductShelfType(Integer businessId, String productCounty, String shelfType) {
        Locale locale = LocaleContextHolder.getLocale();
        List<ProductEntity> productEntities = query.selectAllProduct(businessId, productCounty, shelfType);
        if(productEntities == null || productEntities.size() == 0){
            return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有任何商品信息！",locale);
        }
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,productEntities,locale);
    }

    /**
     * 各级查看自己要审核的商品
     * @param businessId
     * @param productCity
     * @param productCounty
     * @param reviewType
     * @return
     */
    @Override
    public String selectProductReviewType(Integer businessId, String productCity, String productCounty, String reviewType) {
        Locale locale = LocaleContextHolder.getLocale();
        List<ProductEntity> productEntities = query.selectProductCounty(productCity, productCounty, reviewType);
        if(productEntities == null || productEntities.size() == 0){
            return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有任何要审核的商品信息！",locale);
        }
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,productEntities,locale);
    }

    /**
     * 商品审核
     * @param productId
     * @param reviewType
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public String updateReviewType(Integer productId, String reviewType) {
        Locale locale = LocaleContextHolder.getLocale();
        ProductEntity byId = productDao.findById(productId);
        if(byId == null){
            return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有该商品的信息！",locale);
        }
        if(reviewType.equals(byId.getReviewType()) || reviewType.compareTo(byId.getReviewType()) < 0){
            return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"操作错误！",locale);
        }
        if("3".equals(reviewType)){
            String dateTimeToString = DateTimeUtil.dateTimeToString(LocalDateTime.now());
            productDao.updateShelfType(productId,"1",dateTimeToString);
        }
        productDao.updateReviewType(productId,reviewType);
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"操作成功！",locale);
    }
}
