package com.sptg.gateway.controller.implement;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
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.entity.ProjectCompletion;
import com.sptg.estate.api.feigns.ProjectCompletionFeign;
import com.sptg.permissions.api.entity.Account;
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: ProjectCompletionController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-项目竣工控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/project/completion")
public class ProjectCompletionController {

    private final ProjectCompletionFeign projectCompletionFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;

    /**
     * 功能描述:检测项目是否已经创建竣工单记录
     * @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.projectCompletionFeign.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 map{
     *           projectId              :  项目id,
     *           completionDate         :  竣工日期
     *           pillar                 :  立柱机数量
     *           wallHung               :  壁挂机数量
     *           rainCover              :  雨罩数数量
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/insert")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insert(@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;
            }
            String completionDate = map.get("completionDate");
            if(StringUtils.isBlank(completionDate)){
                hashMap.put("result_msg", "completionDate不能为空");
                return hashMap;
            }
            String pillar = map.get("pillar");
            if(StringUtils.isBlank(pillar)){
                hashMap.put("result_msg", "pillar不能为空");
                return hashMap;
            }
            String wallHung = map.get("wallHung");
            if(StringUtils.isBlank(wallHung)){
                hashMap.put("result_msg", "wallHung不能为空");
                return hashMap;
            }
            String rainCover = map.get("rainCover");
            if(StringUtils.isBlank(rainCover)){
                hashMap.put("result_msg", "rainCover不能为空");
                return hashMap;
            }
            ProjectCompletion p = new ProjectCompletion();
            p.setProjectId(Long.valueOf(projectId));
            p.setCompletionDate(new Date(Long.parseLong(completionDate)));
            p.setPillar(Integer.valueOf(pillar));
            p.setWallHung(Integer.valueOf(wallHung));
            p.setRainCover(Integer.valueOf(rainCover));
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            p.setCreateAccountId(onLineUser.getId());
            p.setCreateName(onLineUser.getUserName());
            p.setCreateDate(new Date());
            ResultBean<Boolean> resultBean = this.projectCompletionFeign.insert(p);
            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;
    }
}
