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.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.ProjectDto;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * title: IntegratedServiceController
 * package: com.sptg.estate.biz.security
 * description:  实施项目-控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/implement/project")
public class ProjectController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final ConfFeign confFeign;
    private final ProjectFeign projectFeign;
    private final ContractFeign contractFeign;
    private final ProjectBookFeign projectBookFeign;
    private final ProjectCompletionFeign projectCompletionFeign;
    private final IntegratedServiceFeign integratedServiceFeign;
    private final PositionFeign positionFeign;
    private final ResidentialFeign residentialFeign;

    /**
     * 功能描述:实施项目管理页面
     * @param
     * @return
     *               
     *                     
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("implement/project-manage");
    }

    /**
     * 查询实施项目列表(分页)
     * @param projectDto 查询条件参数
     * @param page 分页参数
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<Project>> showList(ProjectDto projectDto, Page<Project> page){
        //获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        projectDto.setOrgPosition(onLineUser.getOrgPosition());
        projectDto.setPage(page);
        return this.projectFeign.showList(projectDto);
    }

    /**
     * 功能描述:显示新增iframe
     * @param
     * @return
     *               
     *                      
     */
    @GetMapping("/insert/view")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView insertView(){
        ModelAndView modelAndView =  new ModelAndView("implement/project-insert");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:新增项目
     * @param
     * @return
     *               
     *                      
     */
    @PostMapping("/insert")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> insert(Project project){
        //  通过小区id获取项目信息
        ResultBean<Project> resultBean = this.projectFeign.findByResidentialId(project.getResidentialId());
        Project old = null;
        ResultBean<Boolean> r = null;
        if(resultBean.getCode().equals("200")){
            old = resultBean.getData();
            //执行更新操作
            if(old!=null){
                project.setId(old.getId());
                r = this.projectFeign.updateById(project);
            }else{//新增操作
                //获取当前登陆用户
                Account onLineUser = this.securityServiceInterface.getOnLineUser();
                project.setStatus(OperationConstant.ZERO);
                project.setCreateAccountId(onLineUser.getId());
                project.setCreateName(onLineUser.getUserName());
                project.setCreateDate(new Date());
                project.setOrgId(onLineUser.getOrgId());
                project.setRootOrgId(onLineUser.getRootOrgId());
                project.setOrgPosition(onLineUser.getOrgPosition());
                r = this.projectFeign.insert(project);
            }
        }
        return r;
    }
    /**
     * 功能描述:检测小区关联的小区项目数量，一个小区只能创建一个项目
     * @param
     * @return
     *               
     *                      
     */
    @GetMapping("/countByResidentialId")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Boolean> countByResidentialId(Long residentialId){
        ResultBean<Boolean> resultBean = this.projectFeign.countByResidentialId(residentialId);
        return resultBean;
    }

    /**
     * 功能描述:显示新增iframe
     * @param  id 项目id
     * @return  ModelAndView
     *               
     *                      
     */
    @GetMapping("/detail/view")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView detailView(Long id){
        ModelAndView modelAndView =  new ModelAndView("implement/project-detail");
        Project project  = this.projectFeign.findById(id).getData();
        modelAndView.addObject("project", project);
        Contract contract = this.contractFeign.findByProjectId(project.getId()).getData();
        if(contract!=null && contract.getIntegratedServiceId()!=null){
            IntegratedService integratedService = this.integratedServiceFeign.findById(contract.getIntegratedServiceId());
            contract.setIntegratedServiceName(integratedService.getName());
        }
        modelAndView.addObject("contract", contract);
        ResultBean<Position> position = this.positionFeign.findByProjectId(project.getId());
        modelAndView.addObject("position", position.getData());
        ProjectBook projectBook = this.projectBookFeign.findByProjectId(project.getId()).getData();
        modelAndView.addObject("projectBook", projectBook);
        ProjectCompletion projectCompletion = this.projectCompletionFeign.findByProjectId(project.getId()).getData();
        modelAndView.addObject("projectCompletion", projectCompletion);
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:修改项目基础信息
     * @param project
     * @return ResultBean<Boolean>
     *               
     *                      
     */
    @PostMapping("/edit")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_EDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(Project project){
        return this.projectFeign.edit(project);
    }
   
    /**
     * 功能描述:实施项目管理导出
     * @param response
     *             
     *                      
     */
    @RequestMapping(value = "/exportProject")
    @Security(token = EstateSecurityToken.IMPLEMENT_PROJECT_EXPORT,format = DataFormatEnum.VIEW)
    public void exportProject(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/10");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 功能描述:查询小区信息
     * @param residentialId
     *               
     *                      
     */
    @RequestMapping(value = "/findResidentialById")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Residential> findResidentialById(Long residentialId){
        Residential residential = this.residentialFeign.findResidentialById(residentialId);
        return new ResultBean<>(ResultEnum.SUCCESS,residential);
    }

    /**
     * 功能描述:小区列表查询
     * @param residentialDto 小区业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<Residential>>
     *               
     *                      
     */
    @GetMapping(value = "/showResidentialList")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Page<Residential>> showResidentialList(ResidentialDto residentialDto, Page<Residential> page) {
        residentialDto.setPage(page);
        return this.residentialFeign.showResidentialList(residentialDto);
    }
}
