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.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.FaceChargeProjectDto;
import com.sptg.estate.api.entity.FaceChargeProject;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.FaceChargeProjectFeign;
import com.sptg.estate.api.feigns.ResidentialFeign;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * title: UnitController
 * package: com.sptg.gateway.controller.estate
 * description: 人脸套餐 网关控制器
 *
 *             
 *                 
 */
@RestController
@RequestMapping("/estate/faceChargeProject")
@RequiredArgsConstructor
public class FaceChargeProjectController {
    private final FaceChargeProjectFeign faceChargeProjectFeign;
    private final ResidentialFeign residentialFeign;


    /**
     * 功能描述:人脸收费项目 页面初始化
     * @param residentialId 小区ID
     * @return ModelAndView
     *
     */
    @GetMapping("/index/{residentialId}")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(@PathVariable Long residentialId){
        ModelAndView modelAndView = new ModelAndView("estate/faceChargeProject-manage");
        Residential residential = this.residentialFeign.findResidentialById(residentialId);
        modelAndView.addObject("residential",residential);
        return modelAndView;
    }


    /**
     * 功能描述:人脸收费项目 新增页面
     * @param residentialId 小区ID
     * @return ModelAndView
     *
     */
    @GetMapping("/add/{residentialId}/{type}")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView add(@PathVariable Long residentialId, @PathVariable Integer type){
        Residential residential = this.residentialFeign.findResidentialById(residentialId);
        if (residential == null){
            return new ModelAndView("common/404");
        }
        //是否可以添加收费类型为押金的套餐
        boolean b = true;
        ModelAndView modelAndView = new ModelAndView("estate/faceChargeProject-add");
        //如果添加的套餐类型为收费时判断是否有收费类型为套餐的套餐
        if (type == OperationConstant.TWO){
            FaceChargeProjectDto dto = new FaceChargeProjectDto();
            dto.setResidentialId(residentialId);
            dto.setType(type);
            dto.setPutStatus(OperationConstant.ONE);
            List<FaceChargeProject> faceChargeProjects = this.faceChargeProjectFeign.getList(dto);
            if (faceChargeProjects != null && faceChargeProjects.size() > 0){
                b = false;
            }
        }
        modelAndView.addObject("residential",residential);
        modelAndView.addObject("type",type);
        modelAndView.addObject("canAddToll",b);
        return modelAndView;
    }


    /**
     * 功能描述:人脸收费项目 修改页面
     * @param id 套餐ID
     * @return ModelAndView
     *
     */
    @GetMapping("/edit/{id}")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_UPDATE,format = DataFormatEnum.VIEW)
    public ModelAndView edit(@PathVariable Long id){
        FaceChargeProjectDto dto = new FaceChargeProjectDto();
        dto.setId(id);
        FaceChargeProject faceChargeProject = this.faceChargeProjectFeign.getOneFaceChargeProject(dto).getData();
        if (faceChargeProject == null){
            return new ModelAndView("common/404");
        }
        Residential residential = this.residentialFeign.findResidentialById(faceChargeProject.getResidentialId());
        ModelAndView modelAndView = new ModelAndView("estate/faceChargeProject-edit");
        modelAndView.addObject("faceChargeProject",faceChargeProject);
        modelAndView.addObject("residential",residential);
        return modelAndView;
    }


    /**
     * 功能描述:获取全部的人脸收费项目
     * @param dto 查询条件
     * @param page 分页条件
     * @return 返回值
     */
    @GetMapping("/showFaceChargeProjectList")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<FaceChargeProject>> showFaceChargeProjectList(FaceChargeProjectDto dto,Page<FaceChargeProject> page){
        dto.setPage(page);
        return this.faceChargeProjectFeign.showFaceChargeProjectList(dto);
    }

    /**
     * 功能描述:获取全部的人脸收费项目
     * @param residentialId 小区id
     * @return 返回值
     */
    @GetMapping("/getListByResidentialId")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_MANAGE,format = DataFormatEnum.JSON)
    public List<FaceChargeProject> getListByResidentialId(Long residentialId){
        return this.faceChargeProjectFeign.getListByResidentialId(residentialId);
    }

    /**
     * 功能描述:根据小区id 获取全部的人脸收费项目
     * @param residentialId 小区id
     * @return 返回值
     */
    @GetMapping("/selectByResidentialId")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<FaceChargeProject> selectByResidentialId(Long residentialId){
        return this.faceChargeProjectFeign.selectByResidentialId(residentialId);
    }

    /**
     * 功能描述:获取一个收费项目
     * @param dto 查询条件
     * @return 返回值
     */
    @GetMapping("/getOneFaceChargeProject")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<FaceChargeProject> getOneFaceChargeProject(FaceChargeProjectDto dto){
        return this.faceChargeProjectFeign.getOneFaceChargeProject(dto);
    }


    /**
     * 功能描述:获取一个收费项目
     * @param id 人脸项收费项目id
     * @return 返回值
     */
    @GetMapping("/getFaceChargeProjectById")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_MANAGE,format = DataFormatEnum.JSON)
    public FaceChargeProject getFaceChargeProjectById(Long id){
        return this.faceChargeProjectFeign.getFaceChargeProjectById(id);
    }


    /**
     * 功能描述:新增人脸收费项目
     * @param dto 要新增的项目
     * @return ModelAndView
     *
     */
    @PostMapping("/addFaceChargeProject")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addFaceChargeProject(FaceChargeProjectDto dto){
        FaceChargeProject faceChargeProject = this.faceChargeProjectFeign.getOneNotInId(dto);
        Assert.isNull(faceChargeProject,"新增失败，该套餐名称已存在！");
        return this.faceChargeProjectFeign.addFaceChargeProject(dto);
    }


    /**
     * 功能描述:修改人脸收费项目
     * @param dto 要新增的项目
     * @return ModelAndView
     *
     */
    @PostMapping("/updateFaceChargeProject")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateFaceChargeProject(FaceChargeProjectDto dto){
        Assert.notNull(this.faceChargeProjectFeign.getFaceChargeProjectById(dto.getId()),"修改失败，该套餐不存在！");
        FaceChargeProject faceChargeProject = this.faceChargeProjectFeign.getOneNotInId(dto);
        Assert.isNull(faceChargeProject,"修改失败，该套餐名称已存在！");
        return this.faceChargeProjectFeign.updateFaceChargeProject(dto);
    }

    /**
     * 功能描述:修改人脸收费项目
     * @param dto
     * @return ResultBean<Boolean>
     *
     */
    @PostMapping("/putStatusChange")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> putStatusChange(FaceChargeProjectDto dto){
        return this.faceChargeProjectFeign.putStatusChange(dto);
    }

    /**
     * 功能描述:检查是否有上架的套餐
     * @param dto
     * @return ResultBean<Boolean>
     *
     */
    @GetMapping("/checkPutStatus")
    public ResultBean<Boolean> checkPutStatus(FaceChargeProjectDto dto){
        List<FaceChargeProject> list = this.faceChargeProjectFeign.getList(dto);
        if (list != null && list.size() >0){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }

    /**
     * 功能描述:删除人脸收费项目
     * @param dto 要新增的项目
     * @return ModelAndView
     *
     */
    @PostMapping("/deleteFaceChargeProject")
    @Security(token = EstateSecurityToken.ESTATE_FACE_CHARGE_PROJECT_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteFaceChargeProject(FaceChargeProjectDto dto){
        FaceChargeProject faceChargeProject = this.faceChargeProjectFeign.getOneFaceChargeProject(dto).getData();
        Assert.notNull(faceChargeProject,"删除失败，该套餐不存在！");
        return this.faceChargeProjectFeign.deleteFaceChargeProject(dto);
    }
}
