package com.ruoyi.web.controller.project;

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

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.project.BmProject;
import com.ruoyi.system.domain.project.BmProjectStatus;
import com.ruoyi.system.service.project.IBmProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目(添加线索)Controller
 *
 * @author bm
 * @date 2021-03-03
 */
@RestController
@CrossOrigin
@RequestMapping("/bmProject/project")
public class BmProjectController extends BaseController {
    @Autowired
    private IBmProjectService bmProjectService;

    /**
     * 查询项目(添加线索)列表
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmProject bmProject) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isNotNull(loginUser)) {
            List<SysRole> roles = loginUser.getUser().getRoles();
            boolean flag = false;
            if (roles.size() > 0) {
                for (SysRole role : roles) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey) && ("manage".equals(roleKey) || "admin".equals(roleKey))) {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag) {
                Long deptId = loginUser.getUser().getDeptId();
                bmProject.setSysDeptId(deptId.toString());
            }
        }
        List<BmProject> list = bmProjectService.selectBmProjectList(bmProject);
        return getDataTable(list);
    }

    /**
     * 导出项目(添加线索)列表
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:export')")
    @Log(title = "项目(添加线索)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmProject bmProject) {
        List<BmProject> list = bmProjectService.selectBmProjectList(bmProject);
        ExcelUtil<BmProject> util = new ExcelUtil<BmProject>(BmProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 获取项目(添加线索)详细信息
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bmProjectService.selectBmProjectById(id));
    }

    /**
     * 新增项目(添加线索)
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:add')")
    @Log(title = "项目(添加线索)", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody BmProject bmProject) {
        return bmProjectService.insertBmProject(bmProject);
    }

    /**
     * 修改项目(修改线索)
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:edit')")
    @Log(title = "项目(添加线索)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmProject bmProject) {
        return toAjax(bmProjectService.updateBmProject(bmProject));
    }

    /**
     * 删除项目(添加线索)
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:remove')")
    @Log(title = "项目(添加线索)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bmProjectService.deleteBmProjectByIds(ids));
    }


    /**
     * 提交审核
     *
     * @param bmProjectStatus
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:examine')")
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody BmProjectStatus bmProjectStatus) {
        return bmProjectService.examine(bmProjectStatus);
    }

    /**
     * 确认审核
     *
     * @param bmProjectStatus
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:pass')")
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody BmProjectStatus bmProjectStatus) {
        return bmProjectService.examine(bmProjectStatus);
    }

    /**
     * 驳回审核
     *
     * @param bmProjectStatus
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:reject')")
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody BmProjectStatus bmProjectStatus) {
        return bmProjectService.examine(bmProjectStatus);
    }

    /**
     * 撤销审核
     *
     * @param bmProjectStatus
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:back')")
    @PostMapping("/back")
    public AjaxResult back(@RequestBody BmProjectStatus bmProjectStatus) {
        return bmProjectService.examine(bmProjectStatus);
    }

    /**
     * 首页查询待审核列表
     */
    @GetMapping("/toBeReviewedList")
    public TableDataInfo toBeReviewedList(BmProject bmProject) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (ObjectUtil.isNotNull(loginUser)) {
            List<SysRole> roles = loginUser.getUser().getRoles();
            boolean flag = false;
            if (roles.size() > 0) {
                for (SysRole role : roles) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey) && ("manage".equals(roleKey) || "admin".equals(roleKey))) {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag) {
                Long deptId = loginUser.getUser().getDeptId();
                bmProject.setDeptId(deptId);
            }
        }
        List<BmProject> list = bmProjectService.selectToBeReviewedList(bmProject);
        return getDataTable(list);
    }

    /**
     * 统计分析公共查询列表
     */
    @PreAuthorize("@ss.hasPermi('bmProject:project:statistics')")
    @GetMapping("/statistics/{projectStatus}")
    public TableDataInfo statisticsList(@PathVariable Long[] projectStatus) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptId = -1L;
        if (ObjectUtil.isNotNull(loginUser)) {
            List<SysRole> roles = loginUser.getUser().getRoles();
            boolean flag = false;
            if (roles.size() > 0) {
                for (SysRole role : roles) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey) && ("manage".equals(roleKey) || "admin".equals(roleKey))) {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag) {
                deptId = loginUser.getUser().getDeptId();
            }
        }
        List<BmProject> list = bmProjectService.selectStatisticsList(projectStatus, deptId);
        return getDataTable(list);
    }

    /**
     * 统计分析-在谈线索-柱状图
     */
    @PostMapping("/projectEharts")
    public AjaxResult projectEharts(String year) {
        Map<String, Object> stringObjectMap = bmProjectService.selectProjectEharts(year);
        return AjaxResult.success(stringObjectMap);
    }

    /**
     * 统计分析-签约项目-柱状图
     */
    @PostMapping("/projectContractEharts")
    public AjaxResult projectContractEharts(String year) {
        Map<String, Object> stringObjectMap = bmProjectService.selectProjectContractEharts(year);
        return AjaxResult.success(stringObjectMap);
    }

    /**
     * 首页-统计数字(只根据当前登录人的部门)
     */
    @PostMapping("/indexInfo")
    public AjaxResult indexInfo(BmProject bmProject) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptId = -1L;
        if (ObjectUtil.isNotNull(loginUser)) {
            List<SysRole> roles = loginUser.getUser().getRoles();
            boolean flag = false;
            if (roles.size() > 0) {
                for (SysRole role : roles) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey) && ("manage".equals(roleKey) || "admin".equals(roleKey))) {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag) {
                deptId = loginUser.getUser().getDeptId();
            }
        }
        bmProject.setDeptId(deptId);
        return bmProjectService.indexInfo(bmProject);
    }


    /**
     * 通过企查查,查询企业信息
     *
     * @param searchKey
     * @return
     */
    @GetMapping("/qichachaSearch/{searchKey}")
    public AjaxResult qichachaSearch(@PathVariable("searchKey") String searchKey) {
        if (StringUtils.isNotBlank(searchKey)){
            return bmProjectService.qichachaSearch(searchKey);
        }
        return AjaxResult.success();
    }
}
