package com.pcloud.book.push.facade;

import com.pcloud.book.base.exception.BookBizException;
import com.pcloud.book.group.vo.UserBookInfoVO;
import com.pcloud.book.push.biz.SelfPushBiz;
import com.pcloud.book.push.check.PushCheck;
import com.pcloud.book.push.dto.PersonalAppletsRecordDTO;
import com.pcloud.book.push.dto.SelfPushAddParamDTO;
import com.pcloud.book.push.dto.SelfPushRecordDTO;
import com.pcloud.book.push.entity.PersonalApplets;
import com.pcloud.common.core.constant.SystemCode;
import com.pcloud.common.dto.ResponseDto;
import com.pcloud.common.page.PageBeanNew;
import com.pcloud.common.permission.PermissionException;
import com.pcloud.common.utils.SessionUtil;

import org.springframework.beans.factory.annotation.Autowired;
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.Map;

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

/**
 * @描述：个人号群发
 * @作者：zhuyajie
 * @创建时间：9:46 2019/11/27
 * @版本：1.0
 */
@Api(description = "个人号群发")
@RestController("selfPushFacade")
@RequestMapping("selfPushFacade")
public class SelfPushFacade {
    @Autowired
    private PushCheck pushCheck;
    @Autowired
    private SelfPushBiz selfPushBiz;

    @ApiOperation("创建个人号群发")
    @PostMapping("createSelfPush")
    public ResponseDto<?> createSelfPush(@RequestHeader("token") String token, @RequestBody @ApiParam SelfPushAddParamDTO addParamDTO) throws PermissionException {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long partyId = (Long) map.get(SessionUtil.PARTY_ID);
        String systemCode = (String) map.get(SessionUtil.SYSTEM_CODE);
        addParamDTO.setSystemCode(systemCode);
        pushCheck.selfPushParamCheck(addParamDTO);
        if (partyId == 0){
            String isSystem = (String) map.get(SessionUtil.IS_SYSTEM);
            if ("0".equals(isSystem)) {
                partyId = (Long) map.get(SessionUtil.MEMBER_ID);
            }
        }
        selfPushBiz.createSelfPush(partyId, addParamDTO);
        return new ResponseDto<>();
    }

    @ApiOperation("个人号群发记录用户列表")
    @GetMapping("listSelfPushUser")
    public ResponseDto<PageBeanNew<UserBookInfoVO>> listSelfPushUser(
            @RequestHeader("token") String token, @RequestParam("pushId") @ApiParam("群发id") Long pushId,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage, @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage) throws PermissionException {
        SessionUtil.getToken4Redis(token);
        if (null == pushId) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少群发id");
        }
        if (null == currentPage || null == numPerPage) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少分页参数");
        }
        PageBeanNew<UserBookInfoVO> pageBeanNew = selfPushBiz.listSelfPushUser(pushId, currentPage, numPerPage);
        return new ResponseDto<>(pageBeanNew);
    }

    @ApiOperation("个人号群发记录列表")
    @GetMapping("listSelfPushRecord")
    public ResponseDto<PageBeanNew<SelfPushRecordDTO>> listSelfPushRecord(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "startTime", required = false) @ApiParam("开始时间") String startTime,
            @RequestParam(value = "endTime", required = false) @ApiParam("结束时间") String endTime,
            @RequestParam(value = "status", required = false) @ApiParam("发送状态（1发送中2发送成功3发送失败）") Integer status) throws PermissionException {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        if (null == currentPage || null == numPerPage) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少分页参数");
        }
        Long partyId = (Long) map.get(SessionUtil.PARTY_ID);
        if (partyId == 0){
            String isSystem = (String) map.get(SessionUtil.IS_SYSTEM);
            if ("0".equals(isSystem)) {
                partyId = (Long) map.get(SessionUtil.MEMBER_ID);
            }
        }
        PageBeanNew<SelfPushRecordDTO> pageBeanNew = selfPushBiz.listSelfPushRecord(currentPage, numPerPage, startTime, endTime, status,partyId);
        return new ResponseDto<>(pageBeanNew);
    }

    @ApiOperation("根据pushId删除")
    @GetMapping("deleteByPushId")
    public ResponseDto<?> deleteByPushId(@RequestHeader("token") String token,@RequestParam("pushId") Long pushId) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        selfPushBiz.deleteByPushId(pushId);
        return new ResponseDto<>();
    }

    @ApiOperation("修改个人号群发")
    @PostMapping("updateSelfPush")
    public ResponseDto<?> updateSelfPush(@RequestHeader("token") String token, @RequestBody @ApiParam SelfPushAddParamDTO addParamDTO) throws PermissionException {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        Long partyId = (Long) map.get(SessionUtil.PARTY_ID);
        String systemCode = (String) map.get(SessionUtil.SYSTEM_CODE);
        addParamDTO.setSystemCode(systemCode);
        pushCheck.selfPushParamCheck(addParamDTO);
        if (partyId == 0){
            String isSystem = (String) map.get(SessionUtil.IS_SYSTEM);
            if ("0".equals(isSystem)) {
                partyId = (Long) map.get(SessionUtil.MEMBER_ID);
            }
        }
        selfPushBiz.updateSelfPush(partyId, addParamDTO);
        return new ResponseDto<>();
    }

    @ApiOperation("获取个人号群发")
    @GetMapping("getSelfPush")
    public ResponseDto<?> getSelfPush(@RequestHeader("token") String token,@RequestParam("pushId") Long pushId) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        return new ResponseDto<>(selfPushBiz.getSelfPush(pushId));
    }

    @ApiOperation("创建小程序")
    @PostMapping("createPersonalApplets")
    public ResponseDto<?> createPersonalApplets(@RequestHeader("token") String token,@RequestBody @ApiParam PersonalApplets applets) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (applets!=null&&applets.getAppletsId()==null) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺失小程序id");
        }
        selfPushBiz.createPersonalApplets(applets);
        return new ResponseDto<>();
    }

    @ApiOperation("删除小程序")
    @GetMapping("deleteAppletsById")
    public ResponseDto<?> deleteAppletsById(@RequestHeader("token") String token,@RequestParam("id") Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        selfPushBiz.deleteAppletsById(id);
        return new ResponseDto<>();
    }

    @ApiOperation("更新小程序")
    @PostMapping("updatePersonalApplets")
    public ResponseDto<?> updatePersonalApplets(@RequestHeader("token") String token,@RequestBody @ApiParam PersonalApplets applets) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (applets!=null&&applets.getId()==null) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺失小程序主键id");
        }
        selfPushBiz.updatePersonalApplets(applets);
        return new ResponseDto<>();
    }

    @ApiOperation("查询所有小程序")
    @GetMapping("getAllApplets")
    public ResponseDto<?> getAllApplets(@RequestHeader("token") String token,
                                        @RequestParam(value = "appletsId",required = false) @ApiParam("小程序id") String  appletsId,
                                        @RequestParam(value = "content",required = false) @ApiParam("小程序id或备注") String content,
                                        @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
                                        @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == currentPage || null == numPerPage) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少分页参数");
        }
        PageBeanNew<PersonalApplets> pageBeanNew=selfPushBiz.getAllApplets(appletsId,content,currentPage,numPerPage);
        return new ResponseDto<>(pageBeanNew);
    }

    @ApiOperation("根据小程序主键id获取小程序信息")
    @GetMapping("getAppletsById")
    public ResponseDto<?> getAppletsById(@RequestHeader("token") String token,
                                        @RequestParam(value = "id",required = true) @ApiParam("小程序主键id") Long   id
                                        ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);

        PersonalApplets personalApplets=selfPushBiz.getAppletsById(id);
        return new ResponseDto<>(personalApplets);
    }




    @ApiOperation("查询小程序发送记录")
    @GetMapping("getSendAppletsRecords")
    public ResponseDto<PageBeanNew<PersonalAppletsRecordDTO>> getSendAppletsRecords(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "startTime", required = false) @ApiParam("开始时间") String startTime,
            @RequestParam(value = "endTime", required = false) @ApiParam("结束时间") String endTime,
            @RequestParam(value = "status", required = false) @ApiParam("发送状态（1发送中2发送成功3发送失败）") Integer status) throws PermissionException {
        Map<String, Object> map = SessionUtil.getToken4Redis(token);
        if (null == currentPage || null == numPerPage) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少分页参数");
        }
        Long partyId = (Long) map.get(SessionUtil.PARTY_ID);
        if (partyId == 0){
            String isSystem = (String) map.get(SessionUtil.IS_SYSTEM);
            if ("0".equals(isSystem)) {
                partyId = (Long) map.get(SessionUtil.MEMBER_ID);
            }
        }
        PageBeanNew<PersonalAppletsRecordDTO> pageBeanNew = selfPushBiz.getSendAppletsRecords(currentPage, numPerPage, startTime, endTime, status,partyId);
        return new ResponseDto<>(pageBeanNew);
    }

}
