package com.sptg.gateway.controller.implement;

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.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.entity.Device;
import com.sptg.estate.api.dto.DeviceInstallationDto;
import com.sptg.estate.api.entity.DeviceInstallation;
import com.sptg.estate.api.feigns.DeviceInstallationFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: DeviceInstallationController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-设备安装控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/device/installation")
public class DeviceInstallationController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final DeviceInstallationFeign deviceInstallationFeign;
    private final ConfFeign confFeign;

    /**
     * 功能描述: 统计设备安装数量
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/count")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> count(){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取结果失败");
        try {
            //获取登录账号
            Account account = this.securityServiceInterface.getOnLineUser();
            // 统计项目数量
            ResultBean<Integer> resultBean = this.deviceInstallationFeign.countByOrgPosition(account.getOrgPosition());
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("count", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 统计设备是否已经安装
     * @param map{
     *
     * }
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/countBySerialNumber")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> countBySerialNumber(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取失败");
        try {
            String serialNumber = map.get("serialNumber");
            if(StringUtils.isBlank(serialNumber)){
                hashMap.put("result_msg", "设备序列号不能为空");
                return hashMap;
            }
            // 统计项目数量
            ResultBean<Boolean> resultBean = this.deviceInstallationFeign.countBySerialNumber(serialNumber);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("result", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 添加设备安装
     * @param
     * @return ResultBean<hashMap> 返回结果
     *               
     *                     
     */
    @PostMapping(value = "/insert")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insert(@RequestBody DeviceInstallation deviceInstallation){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "添加失败");
        try {
            // 统计项目数量
            ResultBean<Boolean> resultBean = this.deviceInstallationFeign.countBySerialNumber(deviceInstallation.getSerialNumber());
            //设备已经安装
            if(resultBean.getData()){
                hashMap.put("result_msg", "设备已安装，请勿重复添加");
                return hashMap;
            }
            //获取登录账号
            Account account = this.securityServiceInterface.getOnLineUser();
            deviceInstallation.setStatus(OperationConstant.ZERO);
            deviceInstallation.setCreateDate(new Date());
            deviceInstallation.setCreateAccountId(account.getId());
            deviceInstallation.setCreateName(account.getUserName());
            deviceInstallation.setCheckState(OperationConstant.ZERO);
            deviceInstallation.setOrgId(account.getOrgId());
            deviceInstallation.setRootOrgId(account.getRootOrgId());
            deviceInstallation.setOrgPosition(account.getOrgPosition());
            resultBean = this.deviceInstallationFeign.insertEntity(deviceInstallation);
            if(resultBean.getData()){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "添加成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 查询设备安装列表(分页)
     * @param page {
     *              current         :    当前页码,
     *              size            :    数据条数
     *
     *            }
     * @param deviceInstallationDto{
     *            residentialName   :    项目名称
     *         }
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> showList(@RequestBody DeviceInstallationDto deviceInstallationDto, @RequestBody Page<DeviceInstallation> page){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取数据失败");
        try {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            deviceInstallationDto.setOrgPosition(onLineUser.getOrgPosition());
            deviceInstallationDto.setPage(page);
            ResultBean<Page<DeviceInstallation>> resultBean = this.deviceInstallationFeign.showList(deviceInstallationDto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("list", resultBean.getData().getRecords());
                hashMap.put("fastDfsServer", this.confFeign.getFileServerHost());
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }

        return hashMap;
    }

    /**
     * 功能描述: 安装详情
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/detail")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> detail(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取详情失败");
        try {
            String id = map.get("id");
            if(StringUtils.isBlank(id)){
                hashMap.put("result_msg", "id不能为空");
                return hashMap;
            }
            ResultBean<DeviceInstallation> resultBean  = this.deviceInstallationFeign.findEntityById(Long.valueOf(id));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "成功");
                hashMap.put("deviceInstallation", resultBean.getData());
                hashMap.put("fastDfsServer", this.confFeign.getFileServerHost());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 编辑
     * @param
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/edit")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> edit(@RequestBody DeviceInstallation deviceInstallation){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "修改失败");
        try {
            ResultBean<Boolean> resultBean = this.deviceInstallationFeign.edit(deviceInstallation);
            if(resultBean.getData()){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "修改成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:定位id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *                
     *                      
     */
    @PostMapping(value = "/checkState")
    public Map<String,Object> checkState(@RequestBody DeviceInstallationDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "审核失败");
        try {
            //获取审核人信息
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setCheckName(onLineUser.getUserName());
            dto.setCheckAccountId(onLineUser.getId());
            dto.setCheckDate(new Date());
            ResultBean<Boolean> resultBean = this.deviceInstallationFeign.checkState(dto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode()) &&  resultBean.getData()){
                hashMap.put("result_msg", "审核成功");
                hashMap.put("result_code", 1);
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:检测安装环境评审记录是否已经安装设备
     * @param map{
     *           installationReviewId      :       安装环境评审记录id
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/countByInstallationReviewId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> countByInstallationReviewId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "检测失败");
        try {
            String installationReviewId = map.get("installationReviewId");
            if(StringUtils.isBlank(installationReviewId)){
                hashMap.put("result_msg", "installationReviewId不能为空");
                return hashMap;
            }
            ResultBean<Boolean> resultBean = this.deviceInstallationFeign.countByInstallationReviewId(Long.valueOf(installationReviewId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }


    /**
     * 功能描述:根据序列号查寻设备安装记录
     * @param map{
     *           serialNumber      :       设备序列号
     * }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/findBySerialNumber")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findBySerialNumber(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取结果失败");
        try {
            String serialNumber = map.get("serialNumber");
            ResultBean<DeviceInstallation> resultBean = this.deviceInstallationFeign.findBySerialNumber(serialNumber);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("deviceInstallation", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:根据序列号查寻设备安装记录
     * @param map{
     *           residentialId      :       小区id
     *           checkState         :       审核状态：0-待审核 1-审核通过 2-审核不通过
     * }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/findByResidentialId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findByResidentialId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取结果失败");
        try {
            String residentialId = map.get("residentialId");
            String checkState = map.get("checkState");
            DeviceInstallation deviceInstallation = new DeviceInstallation();
            deviceInstallation.setResidentialId(Long.valueOf(residentialId));
            deviceInstallation.setCheckState(Integer.valueOf(checkState));
            ResultBean<DeviceInstallation> resultBean = this.deviceInstallationFeign.findByCondition(deviceInstallation);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("list", resultBean.getList());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }


}
