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

import com.gic.api.base.commons.Constant;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.material.ContentMaterialDTO;
import com.gic.content.api.enums.*;
import com.gic.content.api.qdto.material.ContentMaterialPageQDTO;
import com.gic.content.api.qdto.material.ContentMaterialQDTO;
import com.gic.content.api.qdto.producer.ContentProducerClerkRandomQDTO;
import com.gic.content.api.service.ContentMaterialApiService;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.web.controller.content.adaptor.ClerkStoreAdaptor;
import com.gic.haoban.manage.web.qo.content.ContentAuditMaterialQO;
import com.gic.haoban.manage.web.qo.content.ContentMaterialSaveQO;
import com.gic.haoban.manage.web.vo.content.creative.ContentMaterialDataVO;
import com.gic.haoban.manage.web.vo.content.creative.ContentMaterialVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 好办-创作中心
 *
 */
@Controller
@RequestMapping("/creative")
public class ContentCreativeController {

    private static final Logger log = LoggerFactory.getLogger(ContentCreativeController.class);
    @Autowired
    private ContentMaterialApiService contentMaterialApiService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private ClerkStoreAdaptor clerkStoreAdaptor;

    /**
     * 创建素材
     * @param saveQO
     * @return
     */
    @ResponseBody
    @RequestMapping("/save-material")
    public RestResponse<ContentMaterialDTO> saveMaterial(@Validated @RequestBody ContentMaterialSaveQO saveQO) {
        String clerkId = saveQO.getClerkId();
        StaffDTO staffDTO = clerkStoreAdaptor.queryStaff(clerkId);
        if (staffDTO == null) {
            return RestResponse.failure("-1", "成员不存在");
        }
        ClerkDTO clerk = clerkService.getClerkByClerkId(clerkId);
        if (clerk == null) {
            return RestResponse.failure("-1", "导购不存在");
        }
        //组装导购发布人信息
        ContentProducerClerkRandomQDTO producerQDTO = new ContentProducerClerkRandomQDTO();
        producerQDTO.setProducerType(MaterialProducerTypeEnum.CLERK.value);
        producerQDTO.setNickName(clerk.getClerkName());
        producerQDTO.setAvatar(staffDTO.getNickName());
        producerQDTO.setEnterpriseId(saveQO.getEnterpriseId());
        producerQDTO.setClerkId(clerkId);
        producerQDTO.setClerkCode(clerk.getClerkCode());
        producerQDTO.setStoreId(clerk.getStoreId());
        ContentMaterialQDTO contentMaterialQDTO = EntityUtil.changeEntityByJSON(ContentMaterialQDTO.class, saveQO);
        contentMaterialQDTO.setCreatorId(clerk.getClerkId());
        contentMaterialQDTO.setCreatorName(clerk.getClerkName());
        contentMaterialQDTO.setUpdaterId(clerk.getClerkId());
        contentMaterialQDTO.setUpdaterName(clerk.getClerkName());
        contentMaterialQDTO.setProducer(producerQDTO);
        contentMaterialQDTO.setMaterialPurpose(MaterialPurposeEnum.AUDIT.code);
        contentMaterialQDTO.setPublishType(MaterialPublishTypeEnum.NONE.value);
        contentMaterialQDTO.setShelfStatus(Constant.FLAG_TRUE);
        ServiceResponse<ContentMaterialDTO> response = contentMaterialApiService.saveMaterial(contentMaterialQDTO);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(response.getResult());
    }

    /**
     * 删除素材
     * @param enterpriseId  企业id
     * @param contentMaterialId 素材id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete-material")
    public RestResponse<Void> saveMaterial(String enterpriseId, Long contentMaterialId) {
        ServiceResponse<Void> response = contentMaterialApiService.deleteMaterial(enterpriseId, contentMaterialId);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult();
    }


    /**
     * 素材详情
     * @param enterpriseId 企业id
     * @param contentMaterialId 素材id
     * @return
     */
    @ResponseBody
    @RequestMapping("/detail-material")
    public RestResponse<ContentMaterialVO> getMaterialDetail(String enterpriseId, Long contentMaterialId) {
        ServiceResponse<ContentMaterialDTO> response = contentMaterialApiService.getMaterialDetail(enterpriseId, contentMaterialId);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        ContentMaterialVO detailVO = EntityUtil.changeEntityByJSON(ContentMaterialVO.class, response.getResult());
        return RestResponse.successResult(detailVO);
    }

    /**
     * 素材列表
     * @param auditMaterialQO qo
     * @return
     */
    @ResponseBody
    @RequestMapping("/page-material")
    public RestResponse<Page<ContentMaterialVO>> pageAuditMaterial(@RequestBody ContentAuditMaterialQO auditMaterialQO) {
        ContentMaterialPageQDTO pageQDTO = new ContentMaterialPageQDTO();
        pageQDTO.setEnterpriseId(auditMaterialQO.getEnterpriseId());
        pageQDTO.setStoreId(auditMaterialQO.getStoreId());
        pageQDTO.setClerkId(auditMaterialQO.getClerkId());
        pageQDTO.setQueryScene(MaterialSearchSceneEnum.HAOBAN_AUDIT.value);
        //todo 是否有转化查询
        ServiceResponse<Page<ContentMaterialDTO>> response = contentMaterialApiService.pageMaterial(pageQDTO, auditMaterialQO.getBasePageInfo());
        if (!response.isSuccess() || CollectionUtils.isEmpty(response.getResult().getResult())) {
            return RestResponse.successResult(new Page<>());
        }
        Page<ContentMaterialDTO> page = response.getResult();
        Page<ContentMaterialVO> result = new Page<>();
        List<ContentMaterialVO> list = EntityUtil.changeEntityListByJSON(ContentMaterialVO.class, response.getResult().getResult());
        result.setTotalCount(page.getTotalCount());
        result.setResult(list);
        result.setCurrentPage(page.getCurrentPage());
        result.setPageSize(page.getPageSize());
        //todo 转化数据
        return RestResponse.successResult(result);
    }

    /**
     * 数据概览
     * @param auditMaterialQO  qo
     * @return
     */
    @ResponseBody
    @RequestMapping("/material-data")
    public RestResponse<ContentMaterialDataVO> auditMaterialData(@RequestBody ContentAuditMaterialQO auditMaterialQO) {
        ContentMaterialPageQDTO pageQDTO = new ContentMaterialPageQDTO();
        pageQDTO.setEnterpriseId(auditMaterialQO.getEnterpriseId());
        pageQDTO.setStoreId(auditMaterialQO.getStoreId());
        pageQDTO.setClerkId(auditMaterialQO.getClerkId());
        pageQDTO.setQueryScene(MaterialSearchSceneEnum.HAOBAN_AUDIT.value);
        ServiceResponse<Long> response = contentMaterialApiService.countMaterialBySearch(pageQDTO);
        pageQDTO.setAuditStatus(MaterialAuditEnum.AUDIT.code);
        ServiceResponse<Long> auditResponse = contentMaterialApiService.countMaterialBySearch(pageQDTO);
        if (!response.isSuccess() || !auditResponse.isSuccess()) {
            return RestResponse.failure("-1", "请求失败");
        }
        ContentMaterialDataVO materialDataVO = new ContentMaterialDataVO();
        materialDataVO.setAuditCount(auditResponse.getResult());
        materialDataVO.setCreateCount(response.getResult());
        //todo 转化数据
        return RestResponse.successResult(materialDataVO);
    }

}
