package com.hw.ems.base.controller;

import java.math.BigDecimal;
import java.util.List;

import com.hw.common.core.text.Convert;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.ems.base.domain.BaseMonitorInfo;
import com.hw.ems.base.domain.BasePublicUser;
import com.hw.ems.base.domain.vo.PublicManageVo;
import com.hw.ems.base.service.IBaseMonitorInfoService;
import com.hw.ems.base.service.IBasePublicUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hw.ems.base.domain.BasePublicManage;
import com.hw.ems.base.service.IBasePublicManageService;

import javax.servlet.http.HttpServletResponse;


/**
 * 公摊管理Controller
 *
 * @author ruoyi
 * @date 2023-04-27
 */
@RestController
@RequestMapping("/base/publicsharemanage")
public class BasePublicManageController extends BaseController {
    private String prefix = "base/publicsharemanage";

    @Autowired
    private IBasePublicManageService basePublicManageService;
    @Autowired
    private IBasePublicUserService iBasePublicUserService;
    @Autowired
    private IBaseMonitorInfoService baseMonitorInfoService;

    @RequiresPermissions("base:publicsharemanage:view")
    @GetMapping()
    public String publicsharemanage() {
        return prefix + "/publicsharemanage";
    }

    /**
     * 查询公摊管理列表
     */
    @RequiresPermissions("base:publicsharemanage:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasePublicManage basePublicManage) {
        startPage();
        List<BasePublicManage> list = basePublicManageService.selectBasePublicManageList(basePublicManage);
        return getDataTable(list);
    }

    /**
     * 导出公摊管理列表
     */
    @RequiresPermissions("base:publicsharemanage:export")
    @Log(title = "公摊管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse reponse, BasePublicManage basePublicManage) {
        List<BasePublicManage> list = basePublicManageService.selectBasePublicManageList(basePublicManage);
        ExcelUtil<BasePublicManage> util = new ExcelUtil<BasePublicManage>(BasePublicManage.class);
        util.exportExcel(reponse, list, "公摊管理数据");
    }

    /**
     * 新增公摊管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    @PostMapping("/AddStepTwo")
    @ResponseBody
    public AjaxResult AddStepTwo(PublicManageVo publicManageVo) {
        //公摊表类型 publicShareType 0-公共  1-业主
        AjaxResult ajax = AjaxResult.success();
        /**
         * 计算公摊表数值
         */
        List<BaseMonitorInfo> baseMonitorInfoList = baseMonitorInfoService.selectDistinctPublicMonitor(Convert.toStrArray(publicManageVo.getIds()));
        String[] ids = baseMonitorInfoList.stream().map(BaseMonitorInfo::getMonitorId).toArray(String[]::new);
        if (ids.length == 0){
            return ajax;
        }
        BigDecimal sumPublicExpend = baseMonitorInfoService.queryPublicMonitorExpend(ids, publicManageVo.getStartTime(), publicManageVo.getEndTime(), publicManageVo.getShareType());
        ajax.put("sum", sumPublicExpend);
        //每个用户下的公摊表 求公摊比率
        //勾选用户
        List<BasePublicUser> basePublicUserArrayList = iBasePublicUserService.selectPublicUserList(publicManageVo, sumPublicExpend);
        if (publicManageVo.getShareRule() == 4){
            sumPublicExpend = basePublicUserArrayList.stream().map(BasePublicUser::getPublicExpend).reduce(BigDecimal.ZERO, BigDecimal::add);
            ajax.put("sum", sumPublicExpend);
        }
        ajax.put("basePublicUserList", basePublicUserArrayList);
        ajax.put("startTime", publicManageVo.getStartTime());
        return ajax;
    }

    /**
     * 新增保存公摊管理
     */
    @RequiresPermissions("base:publicsharemanage:add")
    @Log(title = "公摊管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody BasePublicManage basePublicManage) {
        return toAjax(basePublicManageService.insertBasePublicManage(basePublicManage));
    }

    /**
     * 修改公摊管理
     */
    @RequiresPermissions("base:publicsharemanage:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BasePublicManage basePublicManage = basePublicManageService.selectBasePublicManageById(id);
        mmap.put("basePublicManage", basePublicManage);
        return prefix + "/edit";
    }

    /**
     * 修改保存公摊管理
     */
    @RequiresPermissions("base:publicsharemanage:edit")
    @Log(title = "公摊管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasePublicManage basePublicManage) {
        return toAjax(basePublicManageService.updateBasePublicManage(basePublicManage));
    }

    /**
     * 删除公摊管理
     */
    @RequiresPermissions("base:publicsharemanage:remove")
    @Log(title = "公摊管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basePublicManageService.deleteBasePublicManageByIds(ids));
    }
}
