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.qdto.material.ContentMaterialLandingPageQDTO;
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.vo.content.SimpleGoodsInfoVO;
import com.gic.haoban.manage.web.vo.content.share.ContentMaterialLandingPageVO;
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());

        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());
        List<ContentGoodsDTO> goodsInfos = serviceResponse.getResult().getGoodsInfos();
        if (CollectionUtils.isNotEmpty(goodsInfos)) {
            List<SimpleGoodsInfoVO> goodsInfoVOS = goodsInfos.stream()
                    .map(item -> {
                        SimpleGoodsInfoVO temp = new SimpleGoodsInfoVO();
                        BeanUtils.copyProperties(item, temp);
                        temp.setGoodsImg(item.getGoodsImageUrl());
                        return temp;
                    })
                    .collect(Collectors.toList());
            materialLandingPageVO.setGoodsInfos(goodsInfoVOS);
        }
        return RestResponse.successResult(materialLandingPageVO);
    }
}
