package com.ruoyi.project.invoice.controller;

import com.ruoyi.common.utils.StringUtils;
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.TInvoiceDevice;
import com.ruoyi.project.invoice.service.ITInvoiceDeviceService;
import com.ruoyi.project.system.service.ISysDeptService;
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.List;

/**
 * 装置区域管理Controller
 *
 * @author ruoyi
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/invoice/device")
public class TInvoiceDeviceController extends BaseController
{
    @Autowired
    private ITInvoiceDeviceService tInvoiceDeviceService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询装置区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInvoiceDevice tInvoiceDevice)
    {
        startPage();
        List<TInvoiceDevice> list = tInvoiceDeviceService.selectTInvoiceDeviceList(tInvoiceDevice);
        return getDataTable(list);
    }

    /**
     *  查询装置区域下拉列表数据
     */
    @GetMapping("/select")
    public AjaxResult select()
    {

        List<String> list = tInvoiceDeviceService.selectTInvoiceDeviceAllList();

        return AjaxResult.success(list);
    }

    /**
     * 导出装置区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:export')")
    @Log(title = "装置区域管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TInvoiceDevice tInvoiceDevice)
    {
        List<TInvoiceDevice> list = tInvoiceDeviceService.selectTInvoiceDeviceList(tInvoiceDevice);

        for (TInvoiceDevice invoiceDevice : list) {
            if (invoiceDevice.getDeptId()==4L){
                invoiceDevice.setDeptName("N/A");
            }
            String regionalHeadName="";
            for (String userId : invoiceDevice.getRegionalHead().split(",")) {
                regionalHeadName+=sysUserService.selectUserById(Long.parseLong(userId)).getNickName()+",";
            }
            if (StringUtils.isNotEmpty(regionalHeadName)) {
                invoiceDevice.setRegionalHeadName(regionalHeadName.substring(0,regionalHeadName.length()-1));
            }
        }
        ExcelUtil<TInvoiceDevice> util = new ExcelUtil<TInvoiceDevice>(TInvoiceDevice.class);
        return util.exportExcel(list, "device");
    }


    @GetMapping("/listRegionalHead")
    public AjaxResult listRegionalHead(){
        List<TInvoiceDevice> tInvoiceDevices = tInvoiceDeviceService.selectAllRegionalHead(new TInvoiceDevice());
        for (TInvoiceDevice tInvoiceDevice : tInvoiceDevices) {
            StringBuilder nickname = new StringBuilder();
            for (String userId : tInvoiceDevice.getRegionalHead().split(",")) {
                nickname.append(sysUserService.selectUserById(Long.valueOf(userId)).getNickName()).append("，");
            }
            tInvoiceDevice.setRegionalHeadName(nickname.substring(0,nickname.length()-1));
        }
        return AjaxResult.success(tInvoiceDevices);
    }

    /**
     * 获取装置区域管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tInvoiceDeviceService.selectTInvoiceDeviceById(id));
    }

    /**
     * 新增装置区域管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:add')")
    @Log(title = "装置区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInvoiceDevice tInvoiceDevice)
    {
        tInvoiceDevice.setCreaterCode(getUserId().toString());
        return toAjax(tInvoiceDeviceService.insertTInvoiceDevice(tInvoiceDevice));
    }

    /**
     * 修改装置区域管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:edit')")
    @Log(title = "装置区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInvoiceDevice tInvoiceDevice)
    {
        return toAjax(tInvoiceDeviceService.updateTInvoiceDevice(tInvoiceDevice));
    }

    /**
     * 删除装置区域管理
     */
    @PreAuthorize("@ss.hasPermi('invoice:device:remove')")
    @Log(title = "装置区域管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tInvoiceDeviceService.deleteTInvoiceDeviceByIds(ids));
    }
}
