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.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.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * title: AcceptanceController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-竣工验收控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/acceptance")
public class AcceptanceController {

    private final AcceptanceFeign acceptanceFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final ConfFeign confFeign;

    /**
     * 功能描述:检测项目是否已经创建竣工验收记录
     * @param map{
     *           projectId      :       项目id
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/countByProjectId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> countByProjectId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "检测失败");
        try {
            String projectId = map.get("projectId");
            if(StringUtils.isBlank(projectId)){
                hashMap.put("result_msg", "projectId不能为空");
                return hashMap;
            }
            ResultBean<Boolean> resultBean = this.acceptanceFeign.countByProjectId(Long.valueOf(projectId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:添加
     * @param dto
     * @return
     *               
     *                      
     */
    @RequestMapping(value = "/insert")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insert(@RequestBody AcceptanceDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "添加失败");
        try {
            if(dto.getProjectId()==null){
                hashMap.put("result_msg", "projectId不能为空");
                return hashMap;
            }
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setCreateAccountId(onLineUser.getId());
            dto.setCreateName(onLineUser.getUserName());
            dto.setCreateDate(new Date());
            dto.setOrgId(onLineUser.getOrgId());
            dto.setRootOrgId(onLineUser.getRootOrgId());
            dto.setOrgPosition(onLineUser.getOrgPosition());
            dto.setCheckState(OperationConstant.ZERO);
            ResultBean<Boolean> resultBean = this.acceptanceFeign.insert(dto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "添加成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 统计验收记录数量
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/count")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> count(){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取结果失败");
        try {
            //获取登录账号
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            // 统计数量
            ResultBean<Integer> resultBean = this.acceptanceFeign.countByOrgPosition(onLineUser.getOrgPosition());
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("count", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     *  分页查询
     * @param page {
     *              current         :    当前页码,
     *              size            :    登录密码
     *
     *            }
     * @param acceptanceDto{
     *            residentialName   :    项目名称
     *         }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/showList")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> showList(@RequestBody AcceptanceDto acceptanceDto, @RequestBody Page<Acceptance> page){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取数据失败");
        try {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            acceptanceDto.setOrgPosition(onLineUser.getOrgPosition());
            acceptanceDto.setPage(page);
            ResultBean<Page<Acceptance>> resultBean = this.acceptanceFeign.showList(acceptanceDto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("list", resultBean.getData().getRecords());
                hashMap.put("fastDfsServer", this.confFeign.getFileServerHost());
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 编辑
     * @param
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/edit")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> edit(@RequestBody Acceptance acceptance){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "修改失败");
        try {
            ResultBean<Boolean> resultBean = this.acceptanceFeign.edit(acceptance);
            if(resultBean.getData()){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "修改成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:检测id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/checkState")
    public Map<String,Object> checkState(@RequestBody  AcceptanceDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "审核失败");
        try {
            //获取审核人信息
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setCheckName(onLineUser.getUserName());
            dto.setCheckAccountId(onLineUser.getId());
            dto.setCheckDate(new Date());
            ResultBean<Boolean> resultBean = this.acceptanceFeign.checkState(dto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode()) &&  resultBean.getData()){
                hashMap.put("result_msg", "审核成功");
                hashMap.put("result_code", 1);
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
