package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
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.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.AccessRecordsDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import javafx.scene.input.DataFormat;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * title: AccessRecordsController
 * package: com.sptg.gateway.controller.estate
 * description: 出入记录 控制器
 *
 *             
 *                 
 */
@RequestMapping("/estate/accessRecords")
@RestController
@RequiredArgsConstructor
public class AccessRecordsController {

    private final ConfFeign confFeign;
    private final OrgFeign orgFeign;
    private final AccessRecordsFeign accessRecordsFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final RoomFeign roomFeign;
    private final UnitFeign unitFeign;
    private final ResidentialFeign residentialFeign;
    private final UserFeign userFeign;
    private final DeviceFeign deviceFeign;

    /**
     * 功能描述:出入记录页面初始化
     * @return ModelAndView
     *             
     *                       
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_ACCESS_RECORDS_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/access-records-manage");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
 
    /**
     * 功能描述:出入记录列表查询
     * @param accessRecordsDto 出入记录业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<AccessRecords>>
     *             
     *                       
     */
    @GetMapping(value = "/showAccessRecordsList")
    @Security(token = EstateSecurityToken.ESTATE_ACCESS_RECORDS_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<AccessRecords>> showAccessRecordsList(AccessRecordsDto accessRecordsDto, Page<AccessRecords> page) {
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        if (userOrg.getOrgType() == OperationConstant.THREE){
            //物业
            accessRecordsDto.setEstatePosition(onLineUser.getOrgPosition());
        }else{
            //机构
            accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        }
        accessRecordsDto.setPage(page);
        return this.accessRecordsFeign.showAccessRecordsList(accessRecordsDto);
    }


    /**
     * 功能描述:出入记录详情
     * @param id 必填
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = EstateSecurityToken.ESTATE_ACCESS_RECORDS_MANAGE,format = DataFormatEnum.JSON)
    public ModelAndView detail(@PathVariable(value = "id")Long id){
        AccessRecords accessRecords = accessRecordsFeign.findAccessRecordsById(id);
        if (null == id || null == accessRecords){
            //  没有记录
            return new ModelAndView("common/404");
        }
        DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String accessDate = formatter.format(accessRecords.getAccessDate());
        ModelAndView modelAndView = new ModelAndView("estate/access-records-detail");

        if (accessRecords.getRoomId() != null){
            Room room = roomFeign.findRoomById(accessRecords.getRoomId());
            modelAndView.addObject("room",room);
        }else if (accessRecords.getUnitId() != null){
            Unit unit = unitFeign.findUnitById(accessRecords.getUnitId());
            modelAndView.addObject("unit",unit);
        }else if (accessRecords.getResidentialId() != null){
            Residential residential = residentialFeign.findResidentialById(accessRecords.getResidentialId());
            modelAndView.addObject("residential",residential);
        }
        if (accessRecords.getIsAdmin() == OperationConstant.ONE){
            User user = userFeign.findUserById(accessRecords.getOpenUserId());
            if (StringUtils.isEmpty(user.getImageUri())){
                user.setImageUri(null);
            }
            modelAndView.addObject("user",user);
        }
        modelAndView.addObject("user",null);
        modelAndView.addObject("accessRecords",accessRecords);
        modelAndView.addObject("accessDate",accessDate);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     *
     * @param accessRecords
     * 描述：查询出入记录列表
     * @return ResultBean
     * @author lianghanqiang
     * @date
     */
    @PostMapping("/getAccessRecordPage")
    public ResultBean getAccessRecords(@RequestBody AccessRecordsDto accessRecords){
        return  accessRecordsFeign.showAccessRecordsList(accessRecords);
    }
}
