package com.gree.mall.manager.logic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.carouselMap.CarouselMapVO;
import com.gree.mall.manager.commonmapper.CommonPageV2Mapper;
import com.gree.mall.manager.enums.es.CarouselMapPortEnum;
import com.gree.mall.manager.enums.es.ESGoodsStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.CarouselMap;
import com.gree.mall.manager.plus.entity.EsGoods;
import com.gree.mall.manager.plus.entity.Goods;
import com.gree.mall.manager.plus.service.CarouselMapService;
import com.gree.mall.manager.plus.service.EsGoodsService;
import com.gree.mall.manager.plus.service.GoodsService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CarouselMapLogic {

    @Resource
    CarouselMapService carouselMapService;
    @Resource
    GoodsService goodsService;
    @Autowired
    CommonLogic commonLogic;

    @Autowired
    CommonPageV2Mapper commonPageV2Mapper;
    @Resource
    EsGoodsService esGoodsService;

    /**
     * 轮播图列表分页
     */
    public IPage<CarouselMap> page(HttpServletRequest request, Boolean state, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        return carouselMapService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(companyWechatIds), CarouselMap::getCompanyWechatId, companyWechatIds)
                .eq(state != null, CarouselMap::getState, state)
                .orderByDesc(CarouselMap::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
    }
    /**
     * 轮播图详情
     */
    public CarouselMap detail(String id){
        return carouselMapService.getById(id);
    }



    /**
     * 新增轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(HttpServletRequest request, CarouselMap carouselMap) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        //新增轮播
        carouselMap.setCreateTime(new Date());
        carouselMap.setCompanyWechatId(adminUser.getCompanyWechatId());
        carouselMap.setCompanyName(adminUser.getCompanyName());
        carouselMapService.save(carouselMap);
    }

    /**
     * 修改轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CarouselMap carouselMap) {
        //修改轮播
        carouselMapService.updateById(carouselMap);
    }

    /**
     * 删除轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String carouselMapId) {
        carouselMapService.removeById(carouselMapId);
    }

    /**
     * 批量删除轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> carouselMapIds) {
        carouselMapService.removeByIds(carouselMapIds);
    }


    /**
     * 更改轮播图状态
     */
    public void updateState(String carouselMapId, Boolean state) {
        final CarouselMap carouselMap = carouselMapService.getById(carouselMapId);
        // 类型为二手商城并且链接类型为商品的如果商品已售出则提示
        if (carouselMap.getPort().equals(CarouselMapPortEnum.ESHOP.getKey())
                && carouselMap.getType() == 1) {
            if (StringUtils.isNotBlank(carouselMap.getGoodsId())) {
                throw new RemoteServiceException("链接商品为空，操作失败");
            }
            final EsGoods esGoods = esGoodsService.getById(carouselMap.getGoodsId());
            if (Objects.isNull(esGoods)) {
                throw new RemoteServiceException("链接商品不存在，操作失败");
            }
            if (esGoods.getStatus().equals(ESGoodsStatusEnum.SALE.getKey())) {
                throw new RemoteServiceException("链接商品状态“已售出”，操作失败");
            }
        }
        carouselMapService.lambdaUpdate().set(CarouselMap::getState, state)
                .eq(CarouselMap::getId, carouselMapId).update();
    }

    /**
     * 获取商品列表
     * @param goodsId
     * @param goodsName
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<Goods> getGoods(HttpServletRequest request, String goodsId, String goodsName, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        if (StringUtils.isNotBlank(goodsId)) {
            return goodsService.lambdaQuery()
                    .eq(Goods::getGoodsId, goodsId)
                    .in(CollectionUtils.isNotEmpty(companyWechatIds), Goods::getCompanyWechatId, companyWechatIds)
                    .page(new Page<>(pageNum, pageSize));
        }
        return goodsService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(companyWechatIds), Goods::getCompanyWechatId, companyWechatIds)
                .eq(Goods::getStatus, true)
                .like(StringUtils.isNotBlank(goodsName), Goods::getGoodsName, goodsName)
                .page(new Page<>(pageNum, pageSize));
    }

    public IPage<CarouselMapVO> pageV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean,CarouselMapVO.class);
        IPage<CarouselMapVO> carouselMapVOIPage = commonPageV2Mapper.pageV2CarouseMap(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParam
                , companyWechatIds);

        return carouselMapVOIPage;
    }
}
