package com.sptg.gateway.controller.device;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
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.DeviceSecurityToken;
import com.sptg.device.api.dto.FlowCardDto;
import com.sptg.device.api.entity.DevicePath;
import com.sptg.device.api.entity.FlowCard;
import com.sptg.device.api.feigns.FlowCardFeign;
import com.sptg.device.api.feigns.InstructPublishLogFeign;
import com.sptg.estate.api.feigns.UnitFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * title: DeviceController
 * package: com.sptg.gateway.controller.device
 * description: 流量卡控制器
 *
 *
 *
 */
@RestController
@AllArgsConstructor
public class FlowCardController {


    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final FlowCardFeign flowCardFeign;
    private final OrgFeign orgFeign;
    private final UnitFeign unitFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final InstructPublishLogFeign instructPublishLogFeign;

    /**
     * 功能描述: 分页查询设备列表
     * @param dto 查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     *
     *
     */
    @GetMapping(value = "/flowCard/manage/pagingQuery")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<FlowCard>> pagingQuery(FlowCardDto dto, PageBean<FlowCard, FlowCardDto> pageBean){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        if (null != dto.getSelectedId()){
            //  权限校验
            Organization org = this.orgFeign.getOrgById(dto.getSelectedId());
            if (null == org){
                return new ResultBean<>(ResultEnum.SUCCESS,pageBean);
            }
            if (!org.getPosition().startsWith(onLineUser.getOrgPosition())){
                return new ResultBean<>(ResultEnum.FORBIDDEN);
            }
            dto.setOrgPosition(org.getPosition());
        }else {
            dto.setOrgPosition(onLineUser.getOrgPosition());
        }
        pageBean.setDto(dto);
        return this.flowCardFeign.pagingQuery(pageBean);
    }


    /**
     * 功能描述:添加流量卡信息
     * @param dto
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/flowCard/add")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addEntity(FlowCardDto dto){
        if (dto.getOrgId() == null){
            return new ResultBean<>(ResultEnum.ERROR);
        }
        Organization org = this.orgFeign.getOrgById(dto.getOrgId());
        if (org == null){
            return new ResultBean<>(ResultEnum.ERROR);
        }
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setCreateAccountId(onLineUser.getId());
        dto.setOrgName(org.getName());
        dto.setOrgPosition(org.getPosition());
        ResultBean<Boolean> resultBean = this.flowCardFeign.add(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_FLOW_CARD, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),dto,onLineUser,dto);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }


    /**
     * 功能描述: 修改流量卡
     * @param dto 流量卡
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/flowCard/edit")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(FlowCardDto dto){
        FlowCard oldObj = this.flowCardFeign.getById(dto.getId());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        ResultBean<Boolean> resultBean=this.flowCardFeign.edit(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            FlowCard newObj = this.flowCardFeign.getById(dto.getId());
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_PATH_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }


    /**
     * 功能描述: 查询一个流量卡
     * @param dto 查询条件
     * @return ResultBean<Page>
     *
     */
    @GetMapping(value = "/flowCard/manage/getOneFlowCard")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<FlowCard> getOneFlowCard(FlowCardDto dto){
        return new ResultBean<>(ResultEnum.SUCCESS,this.flowCardFeign.getOneFlowCard(dto));
    }


    /**
     * 功能描述:根据id获取流量卡信息
     * @param id
     * @return ResultBean<flowCard>
     *
     *
     */
    @GetMapping(value = "/device/flowCard/get/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<FlowCard> getById(@PathVariable(value = "id") Long id){
        FlowCardDto dto=new FlowCardDto();
        dto.setId(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        FlowCard flowCard = flowCardFeign.getById(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,flowCard);
    }


    /**
     * 功能描述:删除流量卡信息
     * @param id
     * @return ResultBean<Boolean>
     *
     *
     */
    @GetMapping(value = "/device/flowCard/delete/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> delete(@PathVariable(value = "id") Long id){
        FlowCardDto dto=new FlowCardDto();
        dto.setId(id);
        FlowCard oldObj = this.flowCardFeign.getById(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        ResultBean<Boolean> resultBean = this.flowCardFeign.delete(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_FLOW_CARD, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 流量卡绑定设备
     * @param dto 流量卡
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/flowCard/bind")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_BIND,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> bind(FlowCardDto dto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setCreateAccountId(onLineUser.getId());
        return this.flowCardFeign.bind(dto);
    }

    /**
     * 功能描述: 流量卡取消绑定设备
     * @param dto 流量卡
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/flowCard/reBind")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_BIND,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> reBind(FlowCardDto dto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        return this.flowCardFeign.reBind(dto);
    }

    /**
     * 功能描述: 流量卡更换绑定设备
     * @param dto 流量卡
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/flowCard/updateBind")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_BIND,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateBind(FlowCardDto dto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        return this.flowCardFeign.updateBind(dto);
    }
}
