package org.springblade.modules.investment.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.investment.pojo.entity.IbaProject;
import org.springblade.modules.investment.pojo.entity.IbaProjectVolume;
import org.springblade.modules.investment.pojo.query.IbaProjectIncomeQuery;
import org.springblade.modules.investment.pojo.query.IbaProjectQuery;
import org.springblade.modules.investment.pojo.query.IbaProjectVolumeQuery;
import org.springblade.modules.investment.pojo.vo.IbaProjectCompanyTotalVo;
import org.springblade.modules.investment.pojo.vo.IbaProjectTotalVo;
import org.springblade.modules.investment.pojo.vo.IbaProjectTypeTotalVo;
import org.springblade.modules.investment.pojo.vo.incomeTotalVo;
import org.springblade.modules.investment.service.IbaProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投资效益分析-项目表(IbaProject)表控制层
 *
 * @author lihb
 * @since 2025-05-06 10:20:25
 */
@RestController
@RequestMapping("/blade-attach/ibaProject")
public class IbaProjectController {
    /**
     * 服务对象
     */
    @Autowired
    private IbaProjectService ibaProjectService;

    /**
     * 分页查询
     *
     * @param query 分页对象
     * @return 查询结果
     */
    @PostMapping("/page")
    public R<IPage<IbaProject>> queryByPage(@RequestBody IbaProjectQuery query) {
        return R.data(this.ibaProjectService.queryByPage(query));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public R<IbaProject> queryById(@RequestParam Long id) {
        return R.data(this.ibaProjectService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param ibaProject 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public R<IbaProject> add(@RequestBody IbaProject ibaProject) {
        if(StringUtil.isBlank(ibaProject.getProjectName())){
            return R.fail("项目名称不能为空");
        }
        if(StringUtil.isBlank(ibaProject.getOperationCompany())){
            return R.fail("公司不能为空");
        }
        return R.status(this.ibaProjectService.insert(ibaProject));
    }

    /**
     * 编辑数据
     *
     * @param ibaProject 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R<IbaProject> edit(@RequestBody IbaProject ibaProject) {
        if (ibaProject.getId() == null) {
            return R.fail("id不能为空");
        }
        return R.status(this.ibaProjectService.update(ibaProject));
    }

    /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public R<Boolean> deleteById(@RequestParam String ids) {
        if (ids.isEmpty()) {
            return R.fail("id不能为空");
        }
        return R.status(this.ibaProjectService.deleteById(ids));
    }


    /**
     * 统计项目数和公司数
     *
     * @return 统计数量
     */
    @GetMapping("/total")
    public R<IbaProjectTotalVo> total() {
        return R.data(this.ibaProjectService.total());
    }


    /**
     * 根据类型统计
     *
     * @return 统计数量
     */
    @GetMapping("/typeTotal")
    public R<List<IbaProjectTypeTotalVo>> typeTotal() {
        return R.data(this.ibaProjectService.typeTotal());
    }


    /**
     * 根据公司统计
     *
     * @return 统计数量
     */
    @GetMapping("/companyTotal")
    public R<List<IbaProjectCompanyTotalVo>> companyTotal() {
        return R.data(this.ibaProjectService.companyTotal());
    }


    /**
     * 收益统计
     *
     * @return 统计数量
     */
    @PostMapping("/incomeTotalList")
    public R<List<incomeTotalVo>> incomeTotalList(@RequestBody IbaProjectIncomeQuery query) {
        if (query.getType() == null || (query.getType() != 1 && query.getType() != 2)) {
            return R.fail("类型未知");
        }
        return R.data(this.ibaProjectService.incomeTotalList(query));
    }

    /**
     * 车流量统计
     *
     * @return 统计数量
     */
    @PostMapping("/companyList")
    public R<List<IbaProjectVolume>> companyList(@RequestBody IbaProjectVolumeQuery query) {
        if (query.getType() == null || (query.getType() != 1 && query.getType() != 2)) {
            return R.fail("类型未知");
        }

        if(query.getType() == 1 && query.getId() == null){
            return R.fail("项目id为空");
        }
        if(query.getType() == 2 && StringUtils.isEmpty(query.getOperationCompany())){
            return R.fail("公司为空");
        }

        return R.data(this.ibaProjectService.companyList(query));
    }


    /**
     * 根据类型获取项目或者公司的列表
     * @param type 类型 1-项目，2-公司
     * @return 统计数量
     */
    @GetMapping("/selectProjectOrCompanyList")
    public R<List<IbaProject>> selectProjectOrCompanyList(@RequestParam("type") Integer type) {
        return R.data(this.ibaProjectService.selectProjectOrCompanyList(type));
    }



}

