package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
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.estate.api.dto.ServerUserDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.ServerUserFeign;
import com.sptg.estate.api.vo.DevicesAccessPushVO;
import com.sptg.estate.biz.service.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * title: ServerUserController
 * package: com.sptg.estate.biz.controller
 * description: 服务人员-控制器
 *
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class ServerUserController implements ServerUserFeign {

    private final ServerUserServiceInterface serverUserServiceInterface;
    private final ServerUserFaceImageServiceInterface serverUserFaceImageServiceInterface;
    private final AccessCardServiceInterface accessCardServiceInterface;
    private final UnitServiceInterface unitServiceInterface;
    private final ServerUserDetailServiceInterface serverUserDetailServiceInterface;

    @Override
    @PostMapping(value = "/serverUser/showServerUserList")
    public ResultBean<Page<ServerUser>> showServerUserList(@RequestBody ServerUserDto serverUserDto) {
        Page<ServerUser> page = serverUserServiceInterface.showServerUserList(serverUserDto);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping(value = "/serverUser/pagingByServerUserIds")
    public ResultBean<Page<ServerUser>> pagingByServerUserIds(@RequestBody ServerUserDto serverUserDto) {
        ResultBean<Page<ServerUser>> page = serverUserServiceInterface.pagingByServerUserIds(serverUserDto);
        return page;
    }

    @Override
    @PostMapping(value = "/serverUser/insertOrUpdateServerUser")
    public ResultBean<Boolean> insertOrUpdateServerUser(@RequestBody ServerUser serverUser) {
        Long id=serverUser.getId();
        //创建时
        if(id==null||id<=0){
            //启用
            serverUser.setIsEnable(0);
            //待上传人脸
            serverUser.setFaceEnable(OperationConstant.MINUS_ONE);
            //正常状态
            serverUser.setStatus(0);
            //创建时间
            serverUser.setCreateDate(new Date());
        }
        boolean result = serverUserServiceInterface.insertOrUpdate(serverUser);
        if(result){
            this.serverUserDetailServiceInterface.deleteByServerUserId(serverUser.getId());
            String unitIds = serverUser.getUnitIds();
            if(StringUtils.isNotBlank(unitIds)){
               String[] array =  unitIds.split(",");
               for(int i=0;i<array.length;i++){
                   long unitId = Long.valueOf(array[i]);
                   Unit unit = this.unitServiceInterface.findUnitById(unitId);
                   if(null != unit){
                       ServerUserDetail serverUserDetail = new ServerUserDetail();
                       serverUserDetail.setServerUserId(serverUser.getId());
                       serverUserDetail.setUnitId(unitId);
                       serverUserDetail.setUnitName(unit.getName());
                       this.serverUserDetailServiceInterface.insertOrUpdate(serverUserDetail);
                   }
               }
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
    @Override
    @PostMapping(value = "/serverUser/findServerUserById")
    public ResultBean<ServerUser> findServerUserById(@RequestParam(value ="id") long id) {
        ServerUser serverUser = serverUserServiceInterface.findServerUserById(id);
        return new ResultBean<>(ResultEnum.SUCCESS,serverUser);
    }
    @Override
    @PostMapping(value = "/serverUser/deleteServerUser")
    public ResultBean<Boolean> deleteServerUser(@RequestParam(value = "id") long id) {
        ResultBean<Boolean> result = serverUserServiceInterface.deleteServerUser(id);
        return result;
    }
    @Override
    @PostMapping(value = "/serverUser/deleteServerUserList")
    public ResultBean<Boolean> deleteServerUserList(@RequestBody ServerUserDto serverUserDto) {
        return  this.serverUserServiceInterface.deleteServerUserList(serverUserDto);
    }
    @Override
    @PostMapping(value = "/serverUser/updateIsEnable")
    public ResultBean<Boolean> updateIsEnable(@RequestParam(value = "id") long id, @RequestParam(value = "enable") int enable) {
        boolean result = serverUserServiceInterface.updateIsEnable(id,enable);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
    @Override
    @PostMapping(value = "/serverUser/insertServerUserFaceImage")
    public ResultBean<Boolean> insertServerUserFaceImage(@RequestBody ServerUserFaceImage serverUserFaceImage) {
        boolean result= serverUserFaceImageServiceInterface.insert(serverUserFaceImage);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/serverUser/deleteServerUserFaceImage")
    public ResultBean<Boolean> deleteServerUserFaceImage(@RequestBody ServerUserFaceImage serverUserFaceImage){
        Wrapper wrapper = new EntityWrapper();
        if (null != serverUserFaceImage.getServerUserId()){
            wrapper.eq("l_server_user_id",serverUserFaceImage.getServerUserId());
        }
        boolean result = serverUserFaceImageServiceInterface.delete(wrapper);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/serverUser/addOrEditOpenPermission")
    public ResultBean<Boolean> addOrEditOpenPermission(@RequestBody ServerUser serverUser) {
        ResultBean<Boolean> result = serverUserServiceInterface.addOrEditOpenPermission(serverUser);
        return result;
    }
    @Override
    @PostMapping(value = "/serverUser/accessDevicesManualPush")
    public ResultBean<Boolean> accessDevicesManualPush(@RequestBody DevicesAccessPushVO devicesAccessPushVO) {
        ResultBean<Boolean> result = serverUserServiceInterface.accessDevicesManualPush(devicesAccessPushVO);
        return result;
    }
    @Override
    @PostMapping(value = "/serverUser/findServerUserCardAndFaceById")
    public ResultBean<ServerUser> findServerUserCardAndFaceById(@RequestParam(value = "id") long id) {
        ServerUser serverUser = serverUserServiceInterface.findServerUserById(id);
        if(serverUser!=null){
            //查询服务人员人脸图片
            EntityWrapper<ServerUserFaceImage> wrapper=new EntityWrapper<ServerUserFaceImage>();
            wrapper.eq("l_server_user_id",id);
            ServerUserFaceImage serverUserFaceImage=serverUserFaceImageServiceInterface.selectOne(wrapper);
            serverUser.setServerUserFaceImage(serverUserFaceImage);
            //查询服务人员门禁卡信息
            List<AccessCard> accessCardList = accessCardServiceInterface.getAccessCardByUserIdAndType(String.valueOf(id), OperationConstant.FOUR);
            if(null != accessCardList && !accessCardList.isEmpty()){
                serverUser.setAccessCard(accessCardList.get(0));
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,serverUser);
    }

    @Override
    @PostMapping(value = "/serverUser/getServerUserCount")
    public int getServerUserCount(@RequestBody ServerUserDto serverUserDto) {
        return serverUserServiceInterface.getServerUserCount(serverUserDto);
    }

    @Override
    @PostMapping(value = "/serverUser/findServerUserDetailList")
    public List<ServerUserDetail> findServerUserDetailList(@RequestParam(value = "serverUserId") long serverUserId) {
        return this.serverUserDetailServiceInterface.findServerUserDetailList(serverUserId);
    }


    @Override
    @PostMapping(value = "/serverUser/getServerByCondition")
    public List<ServerUser> getServerUserByCondition(@RequestBody ServerUserDto serverUserDto) {
        return this.serverUserServiceInterface.getServerUserByCondition(serverUserDto);
    }

    @Override
    @PostMapping(value = "/serverUser/updateServerUserList")
    public boolean updateServerUserList(@RequestBody List<ServerUser> serverUserList) {
        return this.serverUserServiceInterface.updateBatchById(serverUserList);
    }
}
