package com.ruoyi.web.controller.invest;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.invest.domain.TProjectChannel;
import com.ruoyi.invest.service.ITProjectChannelService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.tool.domain.TUnifyFile;
import com.ruoyi.tool.service.ITUnifyFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 渠道信息Controller
 * 
 * @author ruoyi
 * @date 2024-02-22
 */
@Api(tags = "渠道信息")
@RestController
@RequestMapping("/invest/channel")
public class TProjectChannelController extends BaseController
{
    @Autowired
    private ITProjectChannelService tProjectChannelService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询渠道信息列表
     */
    @ApiOperation("查询渠道信息列表")
    @GetMapping("/list")
    public TableDataInfo list(TProjectChannel tProjectChannel)
    {
        startPage();
        List<TProjectChannel> list = tProjectChannelService.selectTProjectChannelList(tProjectChannel);
        return getDataTable(list);
    }

    /**
     * 导出渠道信息列表
     */
    @ApiOperation("导出渠道信息列表")
    @PreAuthorize("@ss.hasPermi('invest:channel:export')")
    @Log(title = "渠道信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectChannel tProjectChannel)
    {
        List<TProjectChannel> list = tProjectChannelService.selectTProjectChannelList(tProjectChannel)
                .stream().map(n -> {
                    n.setChannelType(dictDataService.selectDictLabel("CHANNEL_TYPE",n.getChannelType()));
                    n.setStatus(dictDataService.selectDictLabel("channel_status",n.getStatus()));
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<TProjectChannel> util = new ExcelUtil<TProjectChannel>(TProjectChannel.class);
        util.exportExcel(response, list, "渠道信息数据");
    }

    /**
     * 获取渠道信息详细信息
     */
    @ApiOperation("获取渠道信息详细信息")
    @PreAuthorize("@ss.hasPermi('invest:channel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectChannelService.selectTProjectChannelById(id));
    }

    /**
     * 新增渠道信息
     */
    @ApiOperation("新增渠道信息")
    @PreAuthorize("@ss.hasPermi('invest:channel:add')")
    @Log(title = "渠道信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectChannel tProjectChannel)
    {
        tProjectChannel.setId(IdUtils.fastSimpleUUID());
        tProjectChannel.setCreateBy(getNickName());
        int number = tProjectChannelService.selectTProjectChannelCode(DateUtils.lastTwoDigits()+"");
        tProjectChannel.setChannelCode(tProjectChannel.getChannelGroup()+"-"+ DateUtils.lastTwoDigits() +"-"+ String.format("%03d",number+1));
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectChannel.getListFile(),
                null,
                tProjectChannel.getId(),//渠道ID
                String.valueOf(FileType.CHANNEL.ordinal()),//文件类型:渠道
                getNickName());

        return toAjax(tProjectChannelService.insertTProjectChannel(tProjectChannel));
    }

    /**
     * 修改渠道信息
     */
    @ApiOperation("修改渠道信息")
    @PreAuthorize("@ss.hasPermi('invest:channel:edit')")
    @Log(title = "渠道信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectChannel tProjectChannel)
    {
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectChannel.getListFile(),
                null,
                tProjectChannel.getId(),//渠道ID
                String.valueOf(FileType.CHANNEL.ordinal()),//文件类型:渠道
                getNickName());

        return toAjax(tProjectChannelService.updateTProjectChannel(tProjectChannel));
    }

    /**
     * 删除渠道信息
     */
    @ApiOperation("删除渠道信息")
    @PreAuthorize("@ss.hasPermi('invest:channel:remove')")
    @Log(title = "渠道信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tProjectChannelService.updateTProjectChannelByIds(ids));
    }
}
