package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.constant.DeviceConstants;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.AccessCardDto;
import com.sptg.estate.api.entity.AccessCard;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * title: CardController
 * package: com.sptg.gateway.controller.estate
 * description: 门禁卡 控制器
 */
@RestController
@RequestMapping("/estate/card")
@RequiredArgsConstructor
public class CardController {

    private final AccessCardFeign accessCardFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

    /**
     * 功能描述:门禁卡管理页面初始化
     *
     * @return ModelAndView
     */
    @RequestMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_CARD_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("estate/card-manage");
        return modelAndView;
    }

    /**
     * 功能描述:门禁卡列表查询
     *
     * @param accessCardDto 门禁卡业务对象（含查询条件）
     * @param page    分页条件
     * @return ResultBean<Page < AccessCard>>
     */
    @GetMapping(value = "/showCardList")
    @Security(token = EstateSecurityToken.ESTATE_CARD_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Page<AccessCard>> showCardList(AccessCardDto accessCardDto, Page<AccessCard> page) {
        accessCardDto.setPage(page);
        ResultBean<Page<AccessCard>> pageResultBean = accessCardFeign.showCardList(accessCardDto);
        return pageResultBean;
    }

    /**
     * 加入黑名单
     * @param id
     * @return
     */
    @GetMapping(value = "/updateBlackCard/{id}")
    @Security(token = EstateSecurityToken.ESTATE_CARD_BLACK_CARD)
    public ResultBean<Boolean> updateBlackCard(@PathVariable("id") Long id){
        AccessCard oldObj = this.accessCardFeign.findAccessCardById(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean = this.accessCardFeign.updateBlackCard(id);
        if(null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            AccessCard newObj = this.accessCardFeign.findAccessCardById(id);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_ACCESS_CARD_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),oldObj,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 取消黑名单
     * @param id
     * @return
     */
    @GetMapping(value = "/cancelBlackCard/{id}")
    @Security(token = EstateSecurityToken.ESTATE_CARD_BLACK_CARD)
    public ResultBean<Boolean> cancelBlackCard(@PathVariable("id") Long id){
        AccessCard oldObj = this.accessCardFeign.findAccessCardById(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean = this.accessCardFeign.cancelBlackCard(id);
        if(null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            AccessCard newObj = this.accessCardFeign.findAccessCardById(id);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_ACCESS_CARD_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),oldObj,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 手动下发不记名门禁卡
     * @param id
     * @return
     */
    @GetMapping(value = "/issueCard/{id}")
    @Security(token = EstateSecurityToken.ESTATE_CARD_ISSUE)
    public ResultBean<Boolean> issueCard(@PathVariable("id") Long id){
        AccessCard accessCard = this.accessCardFeign.findAccessCardById(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        accessCard.setCardEnable(OperationConstant.ONE);
        ResultBean<Boolean> resultBean = this.accessCardFeign.updateAccessCardById(accessCard);
        if(null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            AccessCard newObj = this.accessCardFeign.findAccessCardById(id);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_ACCESS_CARD_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),accessCard,onLineUser,accessCard,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
}
