package com.pcloud.book.applet.facade;

import com.pcloud.appcenter.assist.dto.AssistTempletDTO;
import com.pcloud.book.applet.biz.AppletBookClassifyBiz;
import com.pcloud.book.applet.dto.AppletBookClassifyDTO;
import com.pcloud.book.applet.dto.AppletBookClassifyRelationDTO;
import com.pcloud.book.applet.dto.AppletUserLabelDTO;
import com.pcloud.book.applet.entity.AppletBookClassify;
import com.pcloud.book.applet.entity.AppletBookClassifyAgent;
import com.pcloud.common.dto.ResponseDto;
import com.pcloud.common.utils.SessionUtil;
import com.pcloud.common.utils.cookie.Cookie;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName com.pcloud.book.applet.facade.AppletBookClassifyFacade
 * @Author zhuyajie
 * @Description 小程序书刊分类
 * @Date 2020/5/15 16:27
 * @Version 1.0
 **/
@Api(description = "小程序书刊分类")
@RestController("appletBookClassifyFacade")
@RequestMapping("appletBookClassify")
public class AppletBookClassifyFacade {

    @Autowired
    private AppletBookClassifyBiz appletBookClassifyBiz;

    @ApiOperation("书刊分类查询")
    @GetMapping("getAppletBookClassifyList")
    public ResponseDto<List<AppletBookClassifyDTO>> getAppletBookClassifyList(@RequestParam(value = "showState", required = false) Boolean showState) {
        return new ResponseDto<>(appletBookClassifyBiz.getAppletBookClassifyList(showState));
    }

    @ApiOperation("根据id查询")
    @GetMapping("getById")
    public ResponseDto<AppletBookClassifyDTO> getById(@RequestParam("id") Long id) {
        return new ResponseDto<>(appletBookClassifyBiz.getById(id));
    }

    @ApiOperation("修改书刊分类基本信息")
    @PostMapping("updateBaseInfo")
    public ResponseDto<?> updateBaseInfo(@RequestHeader("token") String token, @RequestBody AppletBookClassify appletBookClassify) {
        SessionUtil.getToken4Redis(token);
        appletBookClassifyBiz.update(appletBookClassify);
        return new ResponseDto<>();
    }

    @ApiOperation("平台端新增客户端标签")
    @PostMapping("addUserLabel")
    public ResponseDto<?> addUserLabel(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("客户端的标签") @Validated AppletUserLabelDTO appletUserLabelDTO) {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long agentId = (Long) map.get(SessionUtil.RAY_AGENT_ID);
        appletUserLabelDTO.setAgentId(agentId);
        appletBookClassifyBiz.addUserLabel(appletUserLabelDTO);
        return new ResponseDto<>();
    }

    @ApiOperation("关联书刊一级分类")
    @PostMapping("addBookTempletRelation")
    public ResponseDto<?> addBookTempletRelation(@RequestBody List<Long> templetIdList, @RequestParam("bookClassifyId") Long bookClassifyId){
        appletBookClassifyBiz.addBookTempletRelation(templetIdList,bookClassifyId);
        return new ResponseDto<>();
    }

    @ApiOperation("根据小程序书刊分类id查书刊一级分类")
    @GetMapping("getRelationBookTempletByBookClassifyId")
    public ResponseDto<List<AppletBookClassifyRelationDTO>> getRelationBookTempletByBookClassifyId(
            @RequestHeader("token") String token, @RequestParam("bookClassifyId") @ApiParam("小程序书刊分类id") Long bookClassifyId){
        SessionUtil.getToken4Redis(token);
        return new ResponseDto<>(appletBookClassifyBiz.getRelationBookTempletByBookClassifyId(bookClassifyId));
    }

    @ApiOperation("平台端修改客户端标签")
    @PostMapping("updateUserLabel")
    public ResponseDto<?> updateUserLabel(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("客户端的标签") @Validated AppletUserLabelDTO appletUserLabelDTO) {
        SessionUtil.getToken4Redis(token);
        appletBookClassifyBiz.updateUserLabel(appletUserLabelDTO);
        return new ResponseDto<>();
    }

    @ApiOperation("平台端删除客户端标签")
    @GetMapping("deleteUserLabel")
    public ResponseDto<?> deleteUserLabel(
            @RequestHeader("token") String token, @RequestParam("id") Long id) {
        SessionUtil.getToken4Redis(token);
        appletBookClassifyBiz.deleteUserLabel(id);
        return new ResponseDto<>();
    }

    @ApiOperation("平台端客户端标签列表")
    @GetMapping("listUserLabel")
    public ResponseDto<List<AppletUserLabelDTO>> listUserLabel(
            @RequestHeader("token") String token, @RequestParam("appletBookClassifyId") Long appletBookClassifyId) {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long agentId = (Long) map.get(SessionUtil.RAY_AGENT_ID);
        List<AppletUserLabelDTO> appletUserLabelDTOS = appletBookClassifyBiz.listUserLabel(appletBookClassifyId, agentId);
        return new ResponseDto<>(appletUserLabelDTOS);
    }


    @ApiOperation("获取书刊分类列表及其标签")
    @GetMapping("getAllBookClassifyAndLabel4Wechat")
    public ResponseDto<List<AppletBookClassifyDTO>> getAllBookClassifyAndLabel4Wechat(
            @CookieValue("userInfo") String userInfo,
            @RequestParam(value = "grayStatus",required = false) String grayStatus){
        Long officialAccountsId = Cookie.getId(userInfo,Cookie._OFFICIAL_ACCOUNTS_ID);
        return new ResponseDto<>(appletBookClassifyBiz.getAllBookClassifyAndLabel4Wechat(grayStatus, officialAccountsId));
    }

    @ApiOperation("客户端标签列表")
    @GetMapping("listUserLabel4Wechat")
    public ResponseDto<List<AppletUserLabelDTO>> listUserLabel4Wechat(
            @CookieValue("userInfo") String userInfo, @RequestParam("appletBookClassifyId") Long appletBookClassifyId) {
        Long officialAccountsId = Cookie.getId(userInfo,Cookie._OFFICIAL_ACCOUNTS_ID);
        List<AppletUserLabelDTO> appletUserLabelDTOS = appletBookClassifyBiz.listUserLabel4Wechat(appletBookClassifyId, officialAccountsId);
        return new ResponseDto<>(appletUserLabelDTOS);
    }

    @ApiOperation("平台端获取二级分类")
    @GetMapping("getChildTempletList")
    public ResponseDto<?> getChildTempletList(@RequestHeader("token") String token, @RequestParam("appletBookClassifyId") Long appletBookClassifyId){
        SessionUtil.getToken4Redis(token);
        List<AssistTempletDTO> list = appletBookClassifyBiz.getChildTempletList(appletBookClassifyId);
        return new ResponseDto<>(list);
    }

    @ApiOperation("小睿出版社修改客户端分类展示状态、名称")
    @PostMapping("updateBaseInfoByAgent")
    public ResponseDto<?> updateBaseInfoByAgent(@RequestHeader("token") String token, @RequestBody @ApiParam AppletBookClassifyAgent appletBookClassifyAgent){
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long agentId = (Long) map.get(SessionUtil.RAY_AGENT_ID);
        appletBookClassifyAgent.setAgentId(agentId);
        appletBookClassifyBiz.updateBaseInfoByAgent(appletBookClassifyAgent);
        return new ResponseDto<>();
    }

    @ApiOperation("小睿出版管理端书刊分类查询")
    @GetMapping("getAppletBookClassifyListByAgent")
    public ResponseDto<List<AppletBookClassifyDTO>> getAppletBookClassifyListByAgent(
            @RequestHeader("token") String token,
            @RequestParam(value = "showState", required = false) Boolean showState) {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long agentId = (Long) map.get(SessionUtil.RAY_AGENT_ID);
        return new ResponseDto<>(appletBookClassifyBiz.getAppletBookClassifyListByAgent(agentId, showState));
    }

    @ApiOperation("客户端书刊分类查询")
    @GetMapping("getAppletBookClassifyList4Wechat")
    public ResponseDto<List<AppletBookClassifyDTO>> getAppletBookClassifyList4Wechat(
            @CookieValue("userInfo") String userInfo,
            @RequestParam(value = "showState", required = false) Boolean showState) {
        Long officialAccountsId = Cookie.getId(userInfo,Cookie._OFFICIAL_ACCOUNTS_ID);
        return new ResponseDto<>(appletBookClassifyBiz.getAppletBookClassifyList4Wechat(officialAccountsId, showState));
    }
}
