package com.hw.ems.base.controller;

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.PageDomain;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.core.web.page.TableSupport;
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.MonitorVo;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公摊用户Controller
 *
 * @author yangwl
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/base/basepublicuser")
public class BasePublicUserController extends BaseController {
    private String prefix = "base/basepublicuser";

    @Autowired
    private IBasePublicUserService basePublicUserService;

    @Autowired
    private IBaseMonitorInfoService baseMonitorInfoService;

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

    /**
     * 查询公摊用户列表
     */
    @RequiresPermissions("base:basepublicuser:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasePublicUser basePublicUser) {
        startPage();
        List<BasePublicUser> list = basePublicUserService.selectBasePublicUserList(basePublicUser);
        return getDataTable(list);
    }

    /**
     * 导出公摊用户列表
     */
    @RequiresPermissions("base:basepublicuser:export")
    @Log(title = "公摊用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, BasePublicUser basePublicUser) {
        List<BasePublicUser> list = basePublicUserService.selectBasePublicUserList(basePublicUser);
        ExcelUtil<BasePublicUser> util = new ExcelUtil<BasePublicUser>(BasePublicUser.class);
        util.exportExcel(response, list, "公摊用户数据");
    }

    /**
     * 新增公摊用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        BaseMonitorInfo baseMonitorInfo = new BaseMonitorInfo();
        baseMonitorInfo.setPublicShareType(0);
        mmap.put("publicShares", baseMonitorInfoService.selectBaseMonitorInfoList(baseMonitorInfo));
        return prefix + "/add";
    }

    /**
     * 新增保存公摊用户
     */
    @RequiresPermissions("base:basepublicuser:add")
    @Log(title = "公摊用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody BasePublicUser basePublicUser) {
        return toAjax(basePublicUserService.insertBasePublicUser(basePublicUser));
    }

    /**
     * 修改公摊用户
     */
    @RequiresPermissions("base:basepublicuser:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BasePublicUser basePublicUser = basePublicUserService.selectBasePublicUserById(id);
        mmap.put("publicShares", baseMonitorInfoService.selectMonitor(id));
        mmap.put("basePublicUser", basePublicUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存公摊用户
     */
    @RequiresPermissions("base:basepublicuser:edit")
    @Log(title = "公摊用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody BasePublicUser basePublicUser) {
        return toAjax(basePublicUserService.updateBasePublicUser(basePublicUser));
    }

    /**
     * 删除公摊用户
     */
    @RequiresPermissions("base:basepublicuser:remove")
    @Log(title = "公摊用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basePublicUserService.deleteBasePublicUserByIds(ids));
    }
    /**
     * 父子表展开
     */

    /**
     * 查询子表数据
     */
    @PostMapping("/childlist")
    @ResponseBody
    public TableDataInfo listChild(@RequestBody BasePublicUser basePublicUser) {
        TableDataInfo rspData = new TableDataInfo();
        List<MonitorVo> lists = basePublicUserService.selectlistChild(basePublicUser.getId());
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (null == pageDomain.getPageNum() || null == pageDomain.getPageSize()) {
            rspData.setRows(lists);
            rspData.setTotal(lists.size());
            return rspData;
        }
        Integer pageNum = (pageDomain.getPageNum() - 1) * 10;
        Integer pageSize = pageDomain.getPageNum() * 10;
        if (pageSize > lists.size()) {
            pageSize = lists.size();
        }
        rspData.setRows(lists.subList(pageNum, pageSize));
        rspData.setTotal(lists.size());
        return rspData;
    }
}
