package com.sptg.gateway.controller.estate;

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.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.EstateRepairDto;
import com.sptg.estate.api.entity.EstateRepair;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.api.feigns.EstateRepairFeign;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import com.sptg.third.api.feigns.grid.GridEstateFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * title: EstateRepairController
 * package: com.sptg.gateway.controller.estate
 * description: 物业报修管理-网关控制器
 *
 *
 *
 */
@RestController
@RequestMapping("/estate/estateRepair")
@RequiredArgsConstructor
public class EstateRepairController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final EstateRepairFeign estateRepairFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final GridEstateFeign gridEstateFeign;
    private final UserFeign userFeign;

    /**
     * 功能描述:物业报修管理页面
     * @return ModelAndView
     *
     *
     */
    @RequestMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/repair-manage");
                modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:展示物业报修列表
     * @param estateRepairDto
     * @param page
     * @return  ResultBean<Page<EstateRepair>>
     *
     *
     */
    @RequestMapping("/showRepairList")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_MANAGE)
    public ResultBean<Page<EstateRepair>> showRepairList(EstateRepairDto estateRepairDto, Page<EstateRepair> page){
        //查询当前用户机构位置
        Account account=securityServiceInterface.getOnLineUser();
        estateRepairDto.setOrgPosition(account.getOrgPosition());
        estateRepairDto.setPage(page);

        return  estateRepairFeign.showEstateRepairList(estateRepairDto);
    }
    /**
     * 功能描述:查询物业报修
     * @param estateRepairDto
     * @return ResultBean<List<EstateRepair>>
     *
     *
     */
    @RequestMapping("/getList")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_MANAGE)
    public ResultBean<List<EstateRepair>> getList(EstateRepairDto estateRepairDto){
        //查询当前用户机构位置
        Account account=securityServiceInterface.getOnLineUser();
        estateRepairDto.setOrgPosition(account.getOrgPosition());
        return  estateRepairFeign.getList(estateRepairDto);
    }
    /**
     * 功能描述:新增页面初始化
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/add")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("estate/repair-add");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:编辑或添加物业报修
     * @param estateRepair
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/addOrEditEstateRepair")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addOrEditEstateRepair( EstateRepair estateRepair){
        //日志事件
        int operationEvent= SysLogConstant.ALTER_EVENT;
        EstateRepair oldObj = new EstateRepair();
        //新增时
        if(estateRepair.getId()==null) {
            //用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
//            estateRepair.setCreateAccountName(onLineUser.getLoginName());
//            estateRepair.setCreateAccountId(onLineUser.getId());
            //机构id
            Long orgId = onLineUser.getOrgId();
            //根机构id
            Long rootOrgId = onLineUser.getRootOrgId();
            estateRepair.setOrgId(orgId);
            estateRepair.setRootOrgId(rootOrgId);
            estateRepair.setOrgPosition(onLineUser.getOrgPosition());
            operationEvent= SysLogConstant.ADD_EVENT;
            oldObj = estateRepair;
        }else{
            ResultBean<EstateRepair> resultBean = this.estateRepairFeign.findEstateRepairById(estateRepair.getId());
            if(resultBean != null && resultBean.getData() != null){
                oldObj = resultBean.getData();
            }
        }

        ResultBean<EstateRepair> resultBean = estateRepairFeign.addOrEditEstateRepair(estateRepair);
        EstateRepair result=resultBean.getData();

        if(result != null){
            EstateRepair newObj = new EstateRepair();
            if (estateRepair.getId() != null){
                newObj = this.estateRepairFeign.findEstateRepairById(estateRepair.getId()).getData();
            }
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_REPAIR_MANAGE, operationEvent,
                    WebUtils.getIp(),estateRepair,account,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/

            /*---------------------------------------------------推送物业报修到网格--------------------------------------------------------*/
            User user = this.userFeign.findUserById(result.getUserId());
            if(user != null){
                result.setUserName(user.getName());
            }

            ResultBean<String> stringResultBean = this.gridEstateFeign.gridEstateRepairPush(result);
            if(stringResultBean.getData().equals("操作成功")){
                SysOperationLog sysOperationLog1=new SysOperationLog("推送物业报修数据到网格成功", SysLogConstant.ADD_EVENT,
                        WebUtils.getIp(),result,account,result);
                sysOperationLogFeign.info(sysOperationLog1);
            }else{
                SysOperationLog sysOperationLog1=new SysOperationLog("推送物业报修数据到网格失败", SysLogConstant.ADD_EVENT,
                        WebUtils.getIp(),result,account,result);
                sysOperationLogFeign.info(sysOperationLog1);
            }
            return new ResultBean<>(ResultEnum.SUCCESS);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }
    /**
     * 功能描述:修改/详细页面
     * @param id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/findEstateRepairById/{id}")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView findEstateRepairById(@PathVariable(value = "id") long id){
        ModelAndView modelAndView = new ModelAndView("estate/repair-add");
        ResultBean<EstateRepair> resultBean=estateRepairFeign.findEstateRepairById(id);
        EstateRepair data = resultBean.getData();
        modelAndView.addObject("bean", data);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }
    /**
     * 功能描述:删除物业报修
     * @param id
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/deleteEstateRepair/{id}")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteEstateRepair(@PathVariable(value = "id") long id){
        EstateRepair oldObj = this.estateRepairFeign.findEstateRepairById(id).getData();
        ResultBean<Boolean> resultBean=this.estateRepairFeign.deleteEstateRepairById(id);
        boolean result=resultBean.getData();
        if(result){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            EstateRepair estateRepair=new EstateRepair();
            estateRepair.setId(id);
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ESTATE_REPAIR_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),estateRepair,account,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
    /**
     * 功能描述:批量删除物业报修
     * @param ids
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/deleteEstateRepairList")
    @Security(token = EstateSecurityToken.ESTATE_REPAIR_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteEstateRepairList(@RequestBody List<Long> ids){
        if (null == ids || ids.size() <=0){
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        EstateRepairDto estateRepairDto = new EstateRepairDto();
        estateRepairDto.setIds(ids);
        ResultBean<Boolean> resultBean=this.estateRepairFeign.deleteEstateRepairList(estateRepairDto);
        boolean result=resultBean.getData();
        if(result) {
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            //登陆用户
            Account account = this.securityServiceInterface.getOnLineUser();
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_REPAIR_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(), estateRepairDto, account);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:修改/详细页面
     * @param id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/findEstateRepairById/app/{id}")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<EstateRepair> findEstateRepairByIdForApp(@PathVariable(value = "id") long id){

        ResultBean<EstateRepair> resultBean=estateRepairFeign.findEstateRepairById(id);

        return resultBean;
    }


    /**
     * 第一次存量推送到数字社区
     *
     * @return
     */
    @RequestMapping("/pushToSzsq")

    public ResultBean<Boolean> pushToSzsq(){
        //查询当前用户机构位置
       EstateRepairDto estateRepairDto = new EstateRepairDto();
       Page<EstateRepair> page = new Page<>();
       page.setCurrent(1);
       page.setSize(100);
        ResultBean<List<EstateRepair>> list = estateRepairFeign.getList(estateRepairDto);
        List<EstateRepair> data = list.getData();
        for(int i=0;i<data.size();i++){
            EstateRepair estateRepair = data.get(i);
            User user = this.userFeign.findUserById(estateRepair.getUserId());
            if(user != null){
                estateRepair.setUserName(user.getName());
            }
            this.gridEstateFeign.gridEstateRepairPush(estateRepair);
        }
        return  null;
    }

}
