package com.sptg.gateway.controller.implement;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
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.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.AcceptanceDto;
import com.sptg.estate.api.entity.Acceptance;
import com.sptg.estate.api.feigns.AcceptanceFeign;
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 com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * title: DeviceTestController
 * package: com.sptg.gateway.controller.implement
 * description: 竣工验收-控制器
 *
 *               
 *                
 */
@RestController
@RequestMapping("/implement/acceptance")
@RequiredArgsConstructor
public class AcceptanceController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final OrgFeign orgFeign;
    private final AcceptanceFeign acceptanceFeign;

    /**
     * 功能描述:设备检测界面
     * @return ModelAndView
     *               
     *                      
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.IMPLEMENT_ACCEPTANCE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("implement/acceptance-manage");
    }

    /**
     * 功能描述:分页查询
     * @param dto
     * @param page
     * @return ResultBean<Page<Acceptance>>
     *               
     *                      
     */
    @PostMapping(value = "/showList")
    @Security(token = EstateSecurityToken.IMPLEMENT_ACCEPTANCE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<Acceptance>> showList(AcceptanceDto dto, Page<Acceptance> page){
        Long selectTreeId = dto.getSelectTreeId();
        String orgPosition="";
        if(selectTreeId==null){
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            orgPosition=onLineUser.getOrgPosition();
        }else{
            //选择树
            Organization organization = this.orgFeign.getOrgById(dto.getSelectTreeId());
            orgPosition=organization.getPosition();
        }
        dto.setOrgPosition(orgPosition);
        //分页
        dto.setPage(page);
        return this.acceptanceFeign.showList(dto);
    }

    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:环境评审id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/checkState")
    @Security(token = EstateSecurityToken.IMPLEMENT_ACCEPTANCE_AUDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> checkState(AcceptanceDto dto){
        //获取审核人信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setCheckName(onLineUser.getUserName());
        dto.setCheckAccountId(onLineUser.getId());
        dto.setCheckDate(new Date());
        return this.acceptanceFeign.checkState(dto);
    }

    /**
     * 功能描述:详情
     * @param id
     * @return ModelAndView
     *               
     *                      
     */
    @GetMapping(value = "/detail/view/{id}")
    @Security(format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable("id") Long id){
        ModelAndView modelAndView = new ModelAndView("implement/acceptance-detail");
        Acceptance bean;
        ResultBean<Acceptance> resultBean = this.acceptanceFeign.findById(id);
        if(resultBean.getData()!=null){
            bean=resultBean.getData();
        }else {
            bean=new Acceptance();
        }
        modelAndView.addObject("bean",bean);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
}
