package com.sptg.gateway.controller.permissions;

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.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.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.dto.AccountDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import com.sptg.permissions.api.feigns.DeviceAdminUserFeign;
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.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * title: DeviceAdminUserController
 * description: 设备管理机账号管理
 *
 *                  
 *                
 */
@RestController
@RequiredArgsConstructor
public class DeviceAdminUserController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final DeviceAdminUserFeign deviceAdminUserFeign;
    private final ResidentialFeign residentialFeign;

    /**
     * @return 设备管理机账号管理页面
     */
    @GetMapping(value = "/permissions/xq/device/admin/user")
    @Security(token = PermissionsSecurityToken.DEVICE_ADMIN_USER)
    public ModelAndView index(){
        return new ModelAndView("permissions/device-admin-user");
    }

    /**
     * 分页查询
     * @param dto 查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/permissions/xq/device/admin/user/pagingQuery")
    @Security(token = PermissionsSecurityToken.DEVICE_ADMIN_USER,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQuery(AccountDto dto, PageBean<DeviceAdminUser,AccountDto> pageBean){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        pageBean.setDto(dto);
        return this.deviceAdminUserFeign.pagingQuery(pageBean);
    }

    /**
     * 新增管理机账号页面
     * @param orgId 机构Id
     * @return ModelAndView
     */
    @GetMapping(value = "/permissions/xq/device/admin/user/add/{orgId}")
    @Security(token = PermissionsSecurityToken.DEVICE_ADMIN_USER)
    public ModelAndView addEntity(@PathVariable(value = "orgId")Long orgId){
        //  获取该机构下的所有小区
        ResidentialDto dto = new ResidentialDto();
        dto.setOrgPid(orgId);
        ResultBean<Residential> resultBean = this.residentialFeign.findListByPosition(dto);

        ModelAndView modelAndView = new ModelAndView("permissions/device-admin-user-add");
        modelAndView.addObject("xqList",resultBean.getList());
        return modelAndView;
    }

    /**
     * 添加管理机账号
     * @param user 账号信息
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/permissions/xq/device/admin/user/add")
    @Security(token = PermissionsSecurityToken.DEVICE_ADMIN_USER)
    public ResultBean<Boolean> addEntity(DeviceAdminUser user){
        Assert.notNull(user,"参数异常");
        Assert.hasText(user.getUserName(),"请输入姓名");
        Assert.notNull(user.getXqId(),"请选择归属小区");
        Assert.hasText(user.getLoginName(),"请输入登录名");
        Assert.hasText(user.getLoginPwd(),"请输入登录密码");
        Residential xq = this.residentialFeign.findResidentialById(user.getXqId());
        Assert.notNull(xq,"小区Id不存在");
        user.setOrgId(xq.getOrgPid());
        user.setOrgName(xq.getOrgPidName());
        user.setXqName(xq.getName());
        user.setOrgPosition(xq.getOrgPosition());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        user.setCreateId(onLineUser.getId());
        ResultBean<Boolean> resultBean = this.deviceAdminUserFeign.addEntity(user);
        //  保存设备管理机账号
        if(null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_ADMIN_USER, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),user,onLineUser,user);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }


}
