package com.gic.haoban.manage.web.controller.content;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.material.ContentGoodsDTO;
import com.gic.content.api.dto.material.ContentMaterialLandingPageInfoDTO;
import com.gic.content.api.dto.material.MaterialLinkDTO;
import com.gic.content.api.dto.material.ProductInfoDTO;
import com.gic.content.api.qdto.material.ContentMaterialLandingPageQDTO;
import com.gic.content.api.qdto.share.MaterialLinkShareQDTO;
import com.gic.content.api.service.ContentMaterialShareApiService;
import com.gic.haoban.manage.api.dto.content.log.ClerkShareMaterialLogDTO;
import com.gic.haoban.manage.api.enums.content.ShareBizType;
import com.gic.haoban.manage.api.service.content.MaterialShareLogApiService;
import com.gic.haoban.manage.web.qo.content.ContentMaterialBaseQO;
import com.gic.haoban.manage.web.qo.content.log.MaterialShareLogQO;
import com.gic.haoban.manage.web.qo.content.share.ContentLinkShareQO;
import com.gic.haoban.manage.web.vo.content.ContentMaterialInfoVO;
import com.gic.haoban.manage.web.vo.content.SimpleGoodsInfoVO;
import com.gic.haoban.manage.web.vo.content.share.ContentMaterialLandingPageVO;
import com.google.common.base.Splitter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 好办-导购分享素材与素材落地页
 */
@RestController
public class ClerkMaterialShareController {

    private static final Logger log = LoggerFactory.getLogger(ClerkMaterialShareController.class);

    @Autowired
    private MaterialShareLogApiService materialShareLogApiService;
    @Autowired
    private ContentMaterialShareApiService contentMaterialShareApiService;

    /**
     * 导购分享素材日志
     *
     * @return
     */
    @RequestMapping(path = "/clerk/share/material/log", method = RequestMethod.POST)
    public RestResponse<Long> clerkShareMaterialLog(@RequestBody MaterialShareLogQO materialShareLogQo) {

        if (materialShareLogQo.getContentMaterialId() == null || StringUtils.isBlank(materialShareLogQo.getClerkId())) {
            log.info("参数缺失 {}", JSON.toJSONString(materialShareLogQo));
            return RestResponse.failure("-1", "参数异常");
        }
        ClerkShareMaterialLogDTO temp = new ClerkShareMaterialLogDTO();
        temp.setEnterpriseId(materialShareLogQo.getEnterpriseId());
        temp.setWxEnterpriseId(materialShareLogQo.getWxEnterpriseId());
        temp.setClerkId(materialShareLogQo.getClerkId());
        temp.setStaffId(materialShareLogQo.getStaffId());
        temp.setShareType(materialShareLogQo.getShareType());
        temp.setBizId(materialShareLogQo.getContentMaterialId() + "");
        temp.setBizType(ShareBizType.MATERIAL.getCode());
        temp.setStoreId(materialShareLogQo.getStoreId());
        temp.setExternalUserId(materialShareLogQo.getExternalUserId());
        temp.setExtend(materialShareLogQo.getExtend());
        ServiceResponse<Long> serviceResponse = materialShareLogApiService.saveClerkShareMaterialLog(temp);
        if (serviceResponse.isSuccess()) {
            return RestResponse.successResult(serviceResponse.getResult());
        }
        return RestResponse.failure("-701", "系统异常");
    }


    /**
     * 查询素材落地页信息
     *
     * @param contentMaterialBaseQO
     * @return
     */
    @RequestMapping(path = "/content/material/share/landing/page/info")
    public RestResponse<ContentMaterialLandingPageVO> queryMaterialLandingPage(ContentMaterialBaseQO contentMaterialBaseQO) {

        ContentMaterialLandingPageQDTO searchQo = new ContentMaterialLandingPageQDTO();
        BeanUtils.copyProperties(contentMaterialBaseQO, searchQo);
        ServiceResponse<ContentMaterialLandingPageInfoDTO> serviceResponse = contentMaterialShareApiService.queryMaterialLandPageInfo(searchQo);
        if (!serviceResponse.isSuccess()) {
            log.info("获取素材落地页异常 {}", JSON.toJSONString(serviceResponse));
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        ContentMaterialLandingPageVO materialLandingPageVO = EntityUtil.changeEntityByJSON(ContentMaterialLandingPageVO.class, serviceResponse.getResult());
        if (serviceResponse.getResult() != null && serviceResponse.getResult().getContentMaterialInfo() != null) {
            ContentMaterialInfoVO contentMaterialInfo = materialLandingPageVO.getContentMaterialInfo();
            if (StringUtils.isNotBlank(serviceResponse.getResult().getContentMaterialInfo().getMaterialImageUrls())) {
                contentMaterialInfo.setMaterialImageUrls(Splitter.on(",").splitToList(serviceResponse.getResult().getContentMaterialInfo().getMaterialImageUrls()));
            }
            if (StringUtils.isNotBlank(serviceResponse.getResult().getContentMaterialInfo().getMaterialThumbnailImageUrls())) {
                contentMaterialInfo.setMaterialThumbnailImageUrls(
                        Splitter.on(",").splitToList(serviceResponse.getResult().getContentMaterialInfo().getMaterialImageUrls()));
            }
        }
        List<ProductInfoDTO> goodsInfos = serviceResponse.getResult().getProductInfos();
        if (CollectionUtils.isNotEmpty(goodsInfos)) {
            List<SimpleGoodsInfoVO> goodsInfoVOS = goodsInfos.stream()
                    .map(item -> {
                        SimpleGoodsInfoVO temp = new SimpleGoodsInfoVO();
                        BeanUtils.copyProperties(item, temp);
                        temp.setGoodsId(item.getProId());
                        temp.setGoodsName(item.getProName());
                        temp.setSalePrice(item.getMinPrice());
                        temp.setGoodsImg(item.getImageUrl());
                        return temp;
                    })
                    .collect(Collectors.toList());
            materialLandingPageVO.setGoodsInfos(goodsInfoVOS);
        }
        return RestResponse.successResult(materialLandingPageVO);
    }

    /**
     * 生成素材外链跳转地址
     * @param materialBaseQO
     * @return
     */
    @RequestMapping(path = "/clerk/share-material-link", method = RequestMethod.POST)
    public RestResponse<MaterialLinkDTO> clerkShareMaterialLink(@RequestBody ContentLinkShareQO linkShareQO) {
        MaterialLinkShareQDTO linkShareQDTO = new MaterialLinkShareQDTO();
        linkShareQDTO.setClerkId(linkShareQO.getClerkId());
        linkShareQDTO.setStoreId(linkShareQO.getStoreId());
        linkShareQDTO.setContentMaterialId(linkShareQO.getContentMaterialId());
        linkShareQDTO.setEnterpriseId(linkShareQO.getEnterpriseId());
        linkShareQDTO.setClerkType(linkShareQO.getClerkType());
        linkShareQDTO.setChannelSource(linkShareQO.getChannelSource());
        ServiceResponse<MaterialLinkDTO> response = contentMaterialShareApiService.generateLink(linkShareQDTO);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(response.getResult());
    }

}
