package com.org.hktshop.controller.system;

import com.org.hktshop.Interface.system.IHktFriendLinkService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.dto.PageQueryDto;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.system.HktFriendLink;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.Ret;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/****
 * 友情链接
 */
//todo 还未完善
@RestController
@Api(tags = "友情链接")
public class HktFriendLinkController extends BaseController {

    @Autowired
    IHktFriendLinkService linkService;

    @PostMapping(value = "/comm/system/queryFriendLinkPage")
    @ApiOperation(value = "查询友情链接分页接口")
    public Object queryLinkPage(@RequestBody HktFriendLink in_link) {
        try {
            Page<HktFriendLink> page = linkService.queryPage(in_link);
            return BaseResult.successWithData(page);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @GetMapping(value = "/comm/system/queryFriendLink")
    @ApiOperation(value = "查询友情链接通用接口")
    public Object queryLinkList() {
        try {
            List<HktFriendLink> list = linkService.queryLists(new HktFriendLink());

            List<Map<String, Object>> mapList = new ArrayList<>();
            for (HktFriendLink link : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("link", link.getLink());
                map.put("logoUrl", link.getLogoUrl());
                map.put("name", link.getName());
                map.put("nofollow", link.getNofollow());

                mapList.add(map);
            }
            return BaseResult.successWithData(mapList);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/system/updateFriendLink")
    @ApiOperation(value = "新增/修改友情链接")
    public Object updateLink(@RequestBody HktFriendLink in_link, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = linkService.updateFriendLink(admin, in_link);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/system/deleteFriendLink")
    @ApiOperation(value = "删除友情链接")
    public Object deleteLink(@RequestBody HktFriendLink in_link, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = linkService.deleteFriendLink(admin, in_link);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}
