package com.ruoyi.project.invoice.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.invoice.domain.TInvoiceUserUnit;
import com.ruoyi.project.invoice.service.ITInvoiceUserUnitService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户单位管理Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@RestController
@RequestMapping("/invoice/unit")
public class TInvoiceUserUnitController extends BaseController
{
    @Autowired
    private ITInvoiceUserUnitService tInvoiceUserUnitService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInvoiceUserUnit tInvoiceUserUnit)
    {
        startPage();
        List<TInvoiceUserUnit> list = tInvoiceUserUnitService.selectTInvoiceUserUnitList(tInvoiceUserUnit);
        return getDataTable(list);
    }
    @GetMapping("/getAllUserUnit")
    public AjaxResult selectAllTInvoiceUserUnit(TInvoiceUserUnit tInvoiceUserUnit)
    {
        return AjaxResult.success(tInvoiceUserUnitService.selectAllTInvoiceUserUnit(tInvoiceUserUnit));
    }

    /**
     * 导出用户单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:export')")
    @Log(title = "用户单位管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TInvoiceUserUnit tInvoiceUserUnit)
    {
        List<TInvoiceUserUnit> list = tInvoiceUserUnitService.selectTInvoiceUserUnitListToExcel(tInvoiceUserUnit);
        for (TInvoiceUserUnit invoiceUserUnit : list) {
            invoiceUserUnit.setDeptName(invoiceUserUnit.getDeptId() == 4L ? "N/A" : invoiceUserUnit.getDeptName());
        }
        ExcelUtil<TInvoiceUserUnit> util = new ExcelUtil<TInvoiceUserUnit>(TInvoiceUserUnit.class);
        return util.exportExcel(list, "unit");
    }

    /**
     * 获取用户单位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tInvoiceUserUnitService.selectTInvoiceUserUnitById(id));
    }

    /**
     * 获取用户单位找到对应的用户主管的信息
     */
    @GetMapping(value = "/getUserByUnit")
    public AjaxResult getInfoByUnit(@RequestParam("unit") String unit)
    {
        List<SysUser> sysUserList = new ArrayList<SysUser>();
        List<TInvoiceUserUnit> tInvoiceUserUnitList = tInvoiceUserUnitService.selectTInvoiceUserUnitByUnit(unit);
        for (TInvoiceUserUnit t : tInvoiceUserUnitList) {
            SysUser sysUser = sysUserService.selectUserById(Long.parseLong(t.getUserIds()));
            if (sysUser != null) {
                sysUserList.add(sysUser);
            }
        }
        return AjaxResult.success(sysUserList);
    }

    /**
     * 新增用户单位管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:add')")
    @Log(title = "用户单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInvoiceUserUnit tInvoiceUserUnit)
    {
        tInvoiceUserUnit.setCreaterCode(getUserId().toString());
        return toAjax(tInvoiceUserUnitService.insertTInvoiceUserUnit(tInvoiceUserUnit));
    }

    /**
     * 修改用户单位管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:edit')")
    @Log(title = "用户单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInvoiceUserUnit tInvoiceUserUnit)
    {
        return toAjax(tInvoiceUserUnitService.updateTInvoiceUserUnit(tInvoiceUserUnit));
    }

    /**
     * 删除用户单位管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:unit:remove')")
    @Log(title = "用户单位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tInvoiceUserUnitService.deleteTInvoiceUserUnitByIds(ids));
    }
}
