package com.slodon.b2b2c.controller.seller.front;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.config.DomainUrlUtil;
import com.slodon.b2b2c.core.constant.ResponseConst;
import com.slodon.b2b2c.core.constant.StoreCateConst;
import com.slodon.b2b2c.core.constant.StoreConst;
import com.slodon.b2b2c.core.constant.VendorConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.TimeUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.SeoTypeConstant;
import com.slodon.b2b2c.goods.dto.SearchProductDTO;
import com.slodon.b2b2c.member.example.MemberFollowStoreExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberFollowStore;
import com.slodon.b2b2c.model.goods.ESGoodsModel;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.goods.StoreCertificateNewListModel;
import com.slodon.b2b2c.model.member.MemberFollowStoreModel;
import com.slodon.b2b2c.model.seller.*;
import com.slodon.b2b2c.seller.example.StoreCertificateExample;
import com.slodon.b2b2c.seller.example.StoreExample;
import com.slodon.b2b2c.seller.example.StoreInnerLabelExample;
import com.slodon.b2b2c.seller.example.VendorExample;
import com.slodon.b2b2c.seller.pojo.*;
import com.slodon.b2b2c.system.pojo.SysSeo;
import com.slodon.b2b2c.vo.goods.SysSeoVO;
import com.slodon.b2b2c.vo.seller.FrontStoreListVO;
import com.slodon.b2b2c.vo.seller.FrontStoreVO;
import com.slodon.b2b2c.vo.seller.StoreCategoryTreeVO;
import com.slodon.b2b2c.vo.seller.StoreGoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @author slodon
 */
@Api(tags = "front-店铺接口")
@RestController
@Slf4j
@RequestMapping("v3/seller/front/store")
public class FrontStoreController {

    @Resource
    private StoreModel storeModel;
    @Resource
    private VendorModel vendorModel;
    @Resource
    private StoreCertificateModel storeCertificateModel;
    @Resource
    private StoreInnerLabelModel storeInnerLabelModel;
    @Resource
    private MemberFollowStoreModel memberFollowStoreModel;
    @Resource
    private GoodsModel goodsModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ESGoodsModel esGoodsModel;
    @Resource
    private StoreCertificateNewListModel storeCertificateNewListModel;

    @Resource
    private StoreProfileModel storeProfileModel;
    @Resource
    private StoreProfileTextListModel storeProfileTextListModel;
    @Resource
    private SellerStoreCompanyShowModel sellerStoreCompanyShowModel;


    @ApiOperation("获取店铺列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "三级分类id", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键词", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序条件：1-销量排序，2-人气排序，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<FrontStoreListVO>> getList(HttpServletRequest request, Integer categoryId,
                                                        @RequestParam(value = "keyword", required = false) String keyword,
                                                        @RequestParam(value = "sort", required = false, defaultValue = "2") Integer sort) {

        Member member = UserUtil.getUser(request, Member.class);
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        List<FrontStoreListVO> vos = new ArrayList<>();

        //满足条件的店铺id
//        Set<Long> storeIdSet = esGoodsModel.searchStoreIdsByEs(categoryId, keyword);
//        if (CollectionUtils.isEmpty(storeIdSet)) {
//            return SldResponse.success(new PageVO<>(vos, pager));
//        }

//        StringBuilder storeIds = new StringBuilder();
//        storeIdSet.forEach(storeId -> {
//            storeIds.append(",").append(storeId);
//        });
        StoreExample storeExample = new StoreExample();
        storeExample.setState(StoreConst.STORE_STATE_OPEN);
//        storeExample.setStoreIdIn(storeIds.substring(1));
        storeExample.setStoreNameLike(keyword);
        if (sort == 2) {
            storeExample.setOrderBy("store_look_volume DESC");
        } else {
            storeExample.setOrderBy("store_sales_volume DESC");
        }

        List<Store> storeList = storeModel.getStoreList(storeExample, pager);
        if (CollectionUtils.isEmpty(storeList)) {
            //没有满足条件的店铺
            return SldResponse.success(new PageVO<>(vos, pager));
        }

//        StringBuilder storeIdIn = new StringBuilder();
//        storeList.forEach(store -> {
//            storeIdIn.append(",").append(store.getStoreId());
//        });
//        //一次性查询这些店铺所有上架的商品
//        List<SearchProductDTO> storeGoodsList = esGoodsModel.getStoreGoodsList(storeIdIn.substring(1), categoryId, keyword);
//
//        //基于店铺处理商品
//        Map<Long/*storeId*/, List<StoreGoodsVO>/*按照销量倒序的商品列表*/> storeGoodsSaleMap = new HashMap<>();
//        Map<Long/*storeId*/, List<StoreGoodsVO>/*按照上架时间倒序的商品列表*/> storeGoodsOnlineMap = new HashMap<>();
//        storeGoodsList.forEach(goods -> {
//            StoreGoodsVO storeGoodsVO = new StoreGoodsVO(goods);
//            //按销量
//            if (storeGoodsSaleMap.containsKey(Long.valueOf(goods.getStoreId()))) {
//                storeGoodsSaleMap.get(Long.valueOf(goods.getStoreId())).add(storeGoodsVO);
//            } else {
//                List<StoreGoodsVO> list = new ArrayList<>();
//                list.add(storeGoodsVO);
//                storeGoodsSaleMap.put(Long.valueOf(goods.getStoreId()), list);
//            }
//
//            //近30天上新
//            Date apartDay = TimeUtil.getDateApartDay(-30);
//            if (apartDay.before(storeGoodsVO.getOnlineTime())) {
//                if (storeGoodsOnlineMap.containsKey(Long.valueOf(goods.getStoreId()))) {
//                    storeGoodsOnlineMap.get(Long.valueOf(goods.getStoreId())).add(storeGoodsVO);
//                } else {
//                    List<StoreGoodsVO> list = new ArrayList<>();
//                    list.add(storeGoodsVO);
//                    storeGoodsOnlineMap.put(Long.valueOf(goods.getStoreId()), list);
//                }
//            }
//        });
//
//        //会员是否收藏
//        Set<Long/*storeId*/> memberFollowStoreSet = new HashSet<>();
//        if (!StringUtil.isNullOrZero(member.getMemberId())) {
//            MemberFollowStoreExample example = new MemberFollowStoreExample();
//            example.setMemberId(member.getMemberId());
//            example.setStoreIdIn(storeIdIn.substring(1));
//            List<MemberFollowStore> memberFollowStoreList = memberFollowStoreModel.getMemberFollowStoreList(example, null);
//            memberFollowStoreList.forEach(memberFollowStore -> {
//                memberFollowStoreSet.add(memberFollowStore.getStoreId());
//            });
//        }

        for (Store store : storeList) {
            //默认店铺logo
            if (StringUtils.isEmpty(store.getStoreLogo())) {
                store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
            }
            //默认店铺背景图片(取移动端横幅)
            if (StringUtils.isEmpty(store.getStoreBannerMobile())) {
                store.setStoreBannerMobile(stringRedisTemplate.opsForValue().get("default_image_store_backdrop"));
            }
            FrontStoreListVO vo = new FrontStoreListVO(store);
            //会员是否收藏
//            vo.setIsFollow(memberFollowStoreSet.contains(store.getStoreId()) ? "true" : "false");

            //查询店铺内的热销商品
//            List<StoreGoodsVO> storeGoodsSaleList = storeGoodsSaleMap.getOrDefault(store.getStoreId(), new ArrayList<>());
//            //按销量倒序
//            storeGoodsSaleList.sort((o1, o2) -> Integer.compare(o2.getActualSales(), o1.getActualSales()));
//            //店铺热销商品个数
//            vo.setHotGoodsNumber(storeGoodsSaleList.size());
//            //店铺热销商品列表
//            vo.setGoodsListVOList(storeGoodsSaleList);

            //查询店铺内本月上新商品
//            List<StoreGoodsVO> storeGoodsOnlineList = storeGoodsOnlineMap.getOrDefault(store.getStoreId(), new ArrayList<>());
            //按创建时间倒序
//            storeGoodsOnlineList.sort((o1, o2) -> o1.getOnlineTime().equals(o2.getOnlineTime()) ? 0 : (o1.getOnlineTime().before(o2.getOnlineTime()) ? 1 : -1));
            //本月上新商品个数
//            vo.setNewGoodsNumber(storeGoodsOnlineList.size());
            //本月上新商品列表
//            vo.setNewGoodsListVOS(storeGoodsOnlineList);
            vos.add(vo);
        }

        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("获取店铺详情接口")
    @OperationLogger(option = "获取店铺详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺id", required = true)
    })
    @GetMapping("detail")
    public JsonResult<FrontStoreVO> detail(HttpServletRequest request, @RequestParam("storeId") Long storeId) throws IOException, WriterException {
        Member member = UserUtil.getUser(request, Member.class);

        Store store = storeModel.getStoreByStoreId(storeId);
        if (store.getState() != StoreConst.STORE_STATE_OPEN) {
            throw new MallException("店铺已关闭", ResponseConst.STATE_STORE_CLOSE);
        }
        //默认店铺logo
        if (StringUtil.isEmpty(store.getStoreLogo())) {
            store.setStoreLogo(stringRedisTemplate.opsForValue().get("default_image_store_logo"));
        }
        //默认店铺背景图片(取移动端横幅)
        if (StringUtil.isEmpty(store.getStoreBannerMobile())) {
            store.setStoreBannerMobile(stringRedisTemplate.opsForValue().get("default_image_store_backdrop"));
        }
        //默认pc端店铺横幅
        if (StringUtils.isEmpty(store.getStoreBannerPc())) {
            store.setStoreBannerPcSource("platform");
            store.setStoreBannerPc(stringRedisTemplate.opsForValue().get("default_image_store_banner_pc"));
        }else{
            store.setStoreBannerPcSource("store");
        }
        FrontStoreVO vo = new FrontStoreVO(store);
        //公司名称
        VendorExample vendorExample = new VendorExample();
        vendorExample.setStoreId(storeId);
        vendorExample.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
        Vendor vendor = vendorModel.getVendorList(vendorExample, null).get(0);
        StoreCertificateExample storeCertificateExample = new StoreCertificateExample();
        storeCertificateExample.setVendorId(vendor.getVendorId());
        List<StoreCertificate> storeCertificateOldList= storeCertificateModel.getStoreCertificateList(storeCertificateExample, null);
        if(!CollectionUtils.isEmpty(storeCertificateOldList)){
            StoreCertificate storeCertificate = storeCertificateOldList.get(0);
            if (!StringUtil.isEmpty(storeCertificate.getCompanyName())) {
                vo.setCompanyName(storeCertificate.getCompanyName());
            }
        }

        //店铺荣誉资质信息(区别于上面的StoreCertificate老表)
        List<StoreCertificateList> storeCertificateList = storeCertificateNewListModel.getStoreCertificateByStoreId(storeId);
        vo.setStoreCertificateList(storeCertificateList);

        //店铺简介图片信息
        List<StoreProfileList> storeProfileList = storeProfileModel.getStoreProfileByStoreIdAndStatus(storeId);
        vo.setStoreProfileList(storeProfileList);
        //店铺简介的内容信息
        StoreProfileTextList storeProfileTextList = storeProfileTextListModel.getList(storeId);
        vo.setStoreProfileTextList(storeProfileTextList);

        //店铺工厂图片信息
        List<StoreCompanyShowList> storeCompanyShowList = sellerStoreCompanyShowModel.getStoreCompanyShowByStoreIdAndStatus(storeId);
        vo.setStoreCompanyShowList(storeCompanyShowList);

        //seo
        SysSeoVO sysSeoVO = storeModel.getRecordByTargetIdAndTypeForStoreDetail(storeId, SeoTypeConstant.SEO_TYPE_SHOP);
        vo.setSeoInfo(sysSeoVO);

        //店铺二维码
        String codeUrl = DomainUrlUtil.SLD_H5_URL + "/standard/store/shopHomePage?vid=" + storeId;
        String qrCode = storeQrCode(codeUrl);
        vo.setStoreQRCode(qrCode);
        vo.setShareLink(codeUrl);

        //会员是否收藏
        if (!StringUtil.isNullOrZero(member.getMemberId())) {
            MemberFollowStoreExample example = new MemberFollowStoreExample();
            example.setMemberId(member.getMemberId());
            example.setStoreId(store.getStoreId());
            List<MemberFollowStore> memberFollowStoreList = memberFollowStoreModel.getMemberFollowStoreList(example, null);
            if (!CollectionUtils.isEmpty(memberFollowStoreList)) {
                vo.setIsFollow("true");
            } else {
                vo.setIsFollow("false");
            }
        } else {
            vo.setIsFollow("false");
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("获取店铺内部商品分类接口")
    @OperationLogger(option = "获取店铺内部商品分类接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺id", required = true)
    })
    @GetMapping("storeCategory")
    public JsonResult<Object> storeCategory(HttpServletRequest request, @RequestParam("storeId") Long storeId) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        List<StoreCategoryTreeVO> tree = new ArrayList<>();
        StoreInnerLabelExample storeInnerLabelExample = new StoreInnerLabelExample();
        storeInnerLabelExample.setParentInnerLabelId(0);
        storeInnerLabelExample.setStoreId(storeId);
        storeInnerLabelExample.setOrderBy("inner_label_sort ASC");
        storeInnerLabelExample.setIsShow(StoreCateConst.STORE_LABEL_IS_SHOW);
        List<StoreInnerLabel> storeInnerLabelList = storeInnerLabelModel.getStoreInnerLabelList(storeInnerLabelExample, pager);
        generateTree(tree, storeInnerLabelList, 2);

        return SldResponse.success(tree);
    }

    /**
     * 递归生成树
     *
     * @param treeList treeList
     * @param data     data
     * @return 返回列表
     */
    private List<StoreCategoryTreeVO> generateTree(List<StoreCategoryTreeVO> treeList, List<StoreInnerLabel> data, Integer grade) {
        if (grade > 0) {
            for (StoreInnerLabel storeInnerLabel : data) {
                StoreCategoryTreeVO tree = new StoreCategoryTreeVO();
                tree.setInnerLabelId(storeInnerLabel.getInnerLabelId());
                tree.setInnerLabelName(storeInnerLabel.getInnerLabelName());
                tree.setInnerLabelSort(storeInnerLabel.getInnerLabelSort());
                tree.setIsShow(storeInnerLabel.getIsShow());
                tree.setParentInnerLabelId(storeInnerLabel.getParentInnerLabelId());
                tree.setCreateTime(storeInnerLabel.getCreateTime());
                tree.setUpdateTime(storeInnerLabel.getUpdateTime());


                StoreInnerLabelExample storeInnerLabelExample = new StoreInnerLabelExample();
                storeInnerLabelExample.setParentInnerLabelId(storeInnerLabel.getInnerLabelId());
                storeInnerLabelExample.setIsShow(StoreCateConst.STORE_LABEL_IS_SHOW);
                storeInnerLabelExample.setOrderBy("inner_label_sort ASC");
                tree.setChildren(generateTree(new ArrayList<>(),
                        storeInnerLabelModel.getStoreInnerLabelList(storeInnerLabelExample, null), grade - 1));
                SysSeoVO sysSeoVO = storeModel.getRecordByTargetIdAndTypeForStoreDetail(Integer.valueOf(tree.getInnerLabelId()).longValue(),
                        SeoTypeConstant.SEO_TYPE_SHOP_CATEGORY);
                tree.setSeoInfo(sysSeoVO);

                treeList.add(tree);
            }
        }
        return treeList;
    }

    /**
     * 生成店铺二维码
     *
     * @param codeUrl 店铺url地址
     * @return 店铺二维码
     * @throws IOException     异常
     * @throws WriterException 异常
     */
    private String storeQrCode(String codeUrl) throws IOException, WriterException {

        //将图片转化成base64进行输出
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        Map<EncodeHintType, String> hints = new HashMap<>(16);
        // 内容所使用编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 300,
                300, hints);
        // 生成二维码
        String codeImg;
        MatrixToImageWriter.writeToStream(bitMatrix, "jpg", byteArrayOutputStream);
        codeImg = new String(Base64.encodeBase64(byteArrayOutputStream.toByteArray()));
        return codeImg;
    }

}
