package com.zd.laboratory.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabWhiteJoinSublist;
import com.zd.laboratory.domain.LabWhitelist;
import com.zd.laboratory.domain.vo.LabSubjectVO;
import com.zd.laboratory.domain.vo.LabWhitelistVO;
import com.zd.laboratory.service.ILabWhiteJoinSublistService;
import com.zd.laboratory.service.ILabWhitelistService;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 白名单Controller
 *
 * @author zd
 * @date 2022-01-26
 */
@RestController
@Api(tags = "【白名单】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
//@ApiSupport(author = Knife4jConfiguration.Author.)
@RequestMapping("/whitelist")
public class LabWhitelistController extends BaseController
{
    @Autowired
    private ILabWhitelistService labWhitelistService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ILabWhiteJoinSublistService labWhiteJoinSublistService;

    /**
     * 查询白名单列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询白名单列表")
    public TableDataInfo<LabWhitelist> list(LabWhitelist labWhitelist)
    {
        startPage();
        List<LabWhitelist> list = labWhitelistService.selectLabWhitelistList(labWhitelist);
        return getDataTable(list);
    }

    /**
     * 导出白名单列表
     */
    @ApiOperation(value = "导出白名单列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.EXPORT)
    @Log(title = "白名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabWhitelist labWhitelist) throws IOException
    {
        List<LabWhitelist> list = labWhitelistService.selectLabWhitelistList(labWhitelist);
        ExcelUtil<LabWhitelist> util = new ExcelUtil<LabWhitelist>(LabWhitelist.class);
        util.exportExcel(response, list, "白名单数据");
    }

    /**
     * 获取白名单详细信息
     */
    @ApiOperation(value = "获取白名单详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<LabWhitelist> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(labWhitelistService.selectLabWhitelistById(id));
    }

    /**
     * 获取白名单用户的实验室列表
     */
    @ApiOperation(value = "获取白名单详细信息")
    @GetMapping(value = "/subList/{userId}")
    public TableDataInfo<LabSubjectVO> getSubList(@PathVariable("userId") Long userId)
    {
        LabWhiteJoinSublist labWhiteJoinSublist = new LabWhiteJoinSublist();
        labWhiteJoinSublist.setWhiteUserId(userId);
        return getDataTable(labWhiteJoinSublistService.selectLabWhiteJoinSublistList(labWhiteJoinSublist));
    }

    /**
     * 新增白名单
     */
    @ApiOperation(value = "新增白名单")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.ADD)
    @Log(title = "白名单", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabWhitelist labWhitelist)
    {
        return ResultData.result(labWhitelistService.insertLabWhitelist(labWhitelist));
    }

    /**
     * 新增白名单 laboratory:whitelist:add
     */
    @ApiOperation(value = "新增白名单")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.ADD)
    @PostMapping("/addList")
    public ResultData addList(@RequestBody LabWhitelistVO labWhitelistVo)
    {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        List<LabWhitelist> labWhitelist=labWhitelistVo.getLabWhitelist();
        int id=0;
        for (int i=0; i<labWhitelist.size(); i++){
            labWhitelist.get(i).setCreateUser(sysUser.getNickName());
            labWhitelist.get(i).setCreateId(sysUser.getUserId());
           id= labWhitelistService.insertLabWhitelist(labWhitelist.get(i));


           for(LabWhiteJoinSublist labWhiteJoinSublist:labWhitelistVo.getLabWhiteJoinSubList()){
               labWhiteJoinSublist.setWhiteUserId(labWhitelist.get(i).getUserId());
               labWhiteJoinSublist.setCreateBy(sysUser.getNickName());
               labWhiteJoinSublist.setCreateTime(new Date());
               labWhiteJoinSublistService.insertLabWhiteJoinSublist(labWhiteJoinSublist);
           }
        }
        return ResultData.result(id);
    }



    /**
     * 修改白名单以及关联的实验室表
     */
    @ApiOperation(value = "修改白名单")
    @PreAuthorize(hasPermi = "laboratory:whitelist:edit")
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @PutMapping("/editWhiteBySubJoin")
    public ResultData editWhiteBySubJoin(@RequestBody LabWhitelistVO labWhitelistVo)
    {
        return  ResultData.result(labWhitelistService.editWhiteBySubJoin(labWhitelistVo));
    }

    /**
     * 修改白名单
     */
    @ApiOperation(value = "修改白名单")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.EDIT)
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabWhitelist labWhitelist)
    {
        return  ResultData.result(labWhitelistService.updateLabWhitelist(labWhitelist));
    }

    /**
     * 删除白名单
     */
    @ApiOperation(value = "删除白名单")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.REMOVE)
    @Log(title = "白名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(labWhitelistService.deleteLabWhitelistByIds(ids));
    }

    /**
     * 根据id删除关联关系
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除白名单关联关系")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WHITELIST + PerFun.REMOVE)
    @Log(title = "白名单", businessType = BusinessType.DELETE)
    @PostMapping("/{id}")
    public ResultData remove(@PathVariable Long id)
    {
        return ResultData.result(labWhitelistService.deleteLabWhitelistById(id));
    }

}
