package com.org.hktshop.controller.user;

import com.aliyun.oss.OSSClient;
import com.org.hktshop.Interface.system.IHktHelperCenterService;
import com.org.hktshop.Interface.system.IHktSysHelpsCategoryService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.system.HktSysHelpes;
import com.org.hktshop.entity.system.HktSysHelpsCategory;
import com.org.hktshop.properties.AliyunOSSProperties;
import com.org.hktshop.service.aliyun.AliyunOssService;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/help")
@Api(tags = "文章管理api")
public class HktUserHelperController {

    @Autowired
    IHktHelperCenterService centerService;
    @Autowired
    IHktSysHelpsCategoryService categoryService;
    @Autowired
    AliyunOSSProperties aliyunOSSProperties;
    @Autowired
    AliyunOssService aliyunOssService;

    @PostMapping("/queryNews")
    @ApiOperation(value = "查询 帮助中心 分类")
    public Object queryNews(@RequestBody HktSysHelpes help) {
        try {
            help.setPageNum(1);
            help.setPageSize(10);
            Page<HktSysHelpes> page = centerService.queryPage(help);

            Map<String, Object> objectMap = new HashMap<>();

            List<Object> objects = new ArrayList<>();
            for (HktSysHelpes h : page.getRecords()) {
                Map<String, Object> subObject = new HashMap<>();
                subObject.put("title", h.getTitle());
                subObject.put("id", h.getId());

                if (!StringUtils.isNull(h.getContent())) {
                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                    h.setContent(aliyunOssService.getTxtMsg(ossClient, h.getContent()));
                }
                subObject.put("content", h.getContent());
                objects.add(subObject);
            }
            objectMap.put("helps", objects);

            return BaseResult.successWithData(objectMap);
        } catch (Exception ex) {
            ex.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }


    @PostMapping("/queryCategory")
    @ApiOperation(value = "查询 帮助中心 分类")
    public Object queryNewsCategory(@RequestBody HktSysHelpsCategory category) {
        try {
            category.setLanguage(null);
            boolean IsGetDetail = !StringUtils.isNull(category.getId());

            List<HktSysHelpsCategory> categories = categoryService.queryLists(category);

            List<Map<String, Object>> tempList = new ArrayList<>();
            for (HktSysHelpsCategory category1 : categories) {
                HktSysHelpes helpe = new HktSysHelpes();
                helpe.setCategoryId(category1.getId());
                helpe.setPageNum(1);
                helpe.setPageSize(10);
                Page<HktSysHelpes> page = centerService.queryPage(helpe);

                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("name", category1.getName());
                objectMap.put("id", category1.getId());
                objectMap.put("img", category1.getImg());

                List<Object> objects = new ArrayList<>();
                for (HktSysHelpes h : page.getRecords()) {
                    Map<String, Object> subObject = new HashMap<>();
                    subObject.put("title", h.getTitle());
                    subObject.put("id", h.getId());

                    if (IsGetDetail) {
                        if (!StringUtils.isNull(h.getContent())) {
                            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                            h.setContent(aliyunOssService.getTxtMsg(ossClient, h.getContent()));
                        }
                        subObject.put("content", h.getContent());
                    }
                    objects.add(subObject);
                }
                objectMap.put("helps", objects);
                tempList.add(objectMap);
            }

            return BaseResult.successWithData(tempList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
