package com.sptg.gateway.controller.implement;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.constant.OperationConstant;
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.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.IntegratedServiceDto;
import com.sptg.estate.api.entity.IntegratedService;
import com.sptg.estate.api.feigns.IntegratedServiceFeign;
import com.sptg.permissions.api.entity.Account;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * title: IntegratedServiceController
 * package: com.sptg.estate.biz.security
 * description:  综合服务-控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/implement/integrated/service")
public class IntegratedServiceController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final IntegratedServiceFeign integratedServiceFeign;
    private final SysOperationLogFeign sysOperationLogFeign;

    /**
     * 功能描述:综合服务管理页面
     * @param
     * @return
     *               
     *                     
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("implement/integrated-service-manage");
    }

    /**
     * 查询综合服务列表(分页)
     * @param integratedServiceDto 查询条件参数
     * @param page 分页参数
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<IntegratedService>> showList(IntegratedServiceDto integratedServiceDto, Page<IntegratedService> page){
        integratedServiceDto.setRootOrgId((long) 1);
        integratedServiceDto.setPage(page);
        return integratedServiceFeign.showList(integratedServiceDto);
    }

    /**
     * 功能描述:显示新增iframe
     *               
     *                      
     */
    @GetMapping("/insert/view")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView insertView(){
        ModelAndView modelAndView = new ModelAndView("implement/integrated-service-insert");
        return modelAndView;
    }

    /**
     * 功能描述:新增
     * @param integratedService
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @PostMapping (value = "/insert")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> insert(IntegratedService integratedService){
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        integratedService.setCreateDate(new Date());
        integratedService.setStatus(OperationConstant.ZERO);
        integratedService.setContractNumber(OperationConstant.ZERO);
        integratedService.setOrgId(onLineUser.getOrgId());
        integratedService.setRootOrgId(onLineUser.getRootOrgId());
        integratedService.setOrgPosition(onLineUser.getOrgPosition());
        //日志事件
        int operationEvent= SysLogConstant.ADD_EVENT;
        //保存
        ResultBean<Boolean>  resultBean=this.integratedServiceFeign.insert(integratedService);
        if(resultBean.getData()!=null && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.IMPLEMENT_INTEGRATED_SERVICE_MANAGE, operationEvent,
                    WebUtils.getIp(),integratedService,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:显示新增iframe
     * @param id
     *               
     *                      
     */
    @GetMapping("/edit/view")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView editView(Long id){
        ModelAndView modelAndView = new ModelAndView("implement/integrated-service-edit");
        IntegratedService integratedService = this.integratedServiceFeign.findById(id);
        modelAndView.addObject("integratedService", integratedService);
        return modelAndView;
    }

    /**
     * 功能描述:修改
     * @param integratedService
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @PostMapping (value = "/edit")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_EDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(IntegratedService integratedService){
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        //日志事件
        int operationEvent= SysLogConstant.ALTER_EVENT;
        //修改
        ResultBean<Boolean>  resultBean=this.integratedServiceFeign.editById(integratedService);
        if(resultBean.getData()!=null && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.IMPLEMENT_INTEGRATED_SERVICE_MANAGE, operationEvent,
                    WebUtils.getIp(),integratedService,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:删除--状态删除
     * @param id
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @GetMapping (value = "/delete/{id}")
    @Security(token = EstateSecurityToken.IMPLEMENT_INTEGRATED_SERVICE_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> delete(@PathVariable(value = "id")Long id ){
        IntegratedService integratedService = new IntegratedService();
        integratedService.setId(id);
        //数据的删除的状态
        integratedService.setStatus(OperationConstant.MINUS_ONE);
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        //日志事件
        int operationEvent= SysLogConstant.DELETE_EVENT;
        //修改
        ResultBean<Boolean>  resultBean=this.integratedServiceFeign.editById(integratedService);
        if(resultBean.getData()!=null && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.IMPLEMENT_INTEGRATED_SERVICE_MANAGE, operationEvent,
                    WebUtils.getIp(),integratedService,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
}
