package com.ruoyi.web.controller.invest;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sms.SmsSenderUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.invest.domain.TProjectCirculation;
import com.ruoyi.invest.service.ITProjectCirculationService;
import com.ruoyi.invest.service.ITProjectPoolService;
import com.ruoyi.system.service.ISysUserService;
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.invest.domain.TProjectApproval;
import com.ruoyi.invest.service.ITProjectApprovalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 立项申请Controller
 * 
 * @author zjc
 * @date 2024-04-02
 */
@Api(tags = "立项申请")
@RestController
@RequestMapping("/invest/approval")
public class TProjectApprovalController extends BaseController
{
    @Autowired
    private ITProjectApprovalService tProjectApprovalService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ITProjectPoolService tProjectPoolService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    /**
     * 查询立项申请列表
     */
    @ApiOperation("查询立项申请列表")
    @PreAuthorize("@ss.hasPermi('invest:approval:list')")
    @GetMapping("/list")
    public TableDataInfo list(TProjectApproval tProjectApproval)
    {
        startPage();
        List<TProjectApproval> list = tProjectApprovalService.selectTProjectApprovalList(tProjectApproval);
        return getDataTable(list);
    }

    /**
     * 导出立项申请列表
     */
    @ApiOperation("导出立项申请列表")
    @PreAuthorize("@ss.hasPermi('invest:approval:export')")
    @Log(title = "立项申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectApproval tProjectApproval)
    {
        List<TProjectApproval> list = tProjectApprovalService.selectTProjectApprovalList(tProjectApproval);
        ExcelUtil<TProjectApproval> util = new ExcelUtil<TProjectApproval>(TProjectApproval.class);
        util.exportExcel(response, list, "立项申请数据");
    }

    /**
     * 获取立项申请详细信息
     */
    @ApiOperation("获取立项申请详细信息")
    @PreAuthorize("@ss.hasPermi('invest:approval:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectApprovalService.selectTProjectApprovalById(id));
    }

    /**
     * 新增立项申请
     */
    @ApiOperation("新增立项申请")
    @Log(title = "立项申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectApproval tProjectApproval)
    {
        tProjectApproval.setId(IdUtils.fastSimpleUUID());

        // todo 保存立项资料
        tUnifyFileService.insertTUnifyFileList(tProjectApproval.getListFile(),
                tProjectApproval.getProjectPoolId(),
                tProjectApproval.getId(),
                String.valueOf(FileType.APPROVAL.ordinal()),
                getNickName());

        // todo 更改立项状态
        tProjectPoolService.updateTProjectPoolByApprovalFlag(tProjectApproval.getProjectPoolId(),"1");

        // todo 短信提醒
        if(StringUtils.isNotEmpty(tProjectApproval.getParticipantsId())){
            StringUtils.str2List(tProjectApproval.getParticipantsId(),",",true,true).stream().forEach(str -> {
                SysUser user = sysUserService.selectUserById(Long.valueOf(str));
                String contents = "您好，"+user.getNickName()+"，" + tProjectApproval.getProjectName() + "已发起立项申请。";
                try {
                    SmsSenderUtils.sendSms(user.getPhonenumber(),contents);
                } catch (IOException e) {
                    logger.error("短信发送失败"+e.getMessage());
                }
            });
        }

        // todo 增加项目创建记录
        tProjectCirculationService.insertTProjectCirculation(tProjectApproval.getProjectPoolId(),"立项申请",getNickName());

        return toAjax(tProjectApprovalService.insertTProjectApproval(tProjectApproval));
    }

    /**
     * 修改立项申请
     */
    @ApiOperation("修改立项申请")
    @PreAuthorize("@ss.hasPermi('invest:approval:edit')")
    @Log(title = "立项申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectApproval tProjectApproval)
    {
        // todo 保存立项资料
        tUnifyFileService.insertTUnifyFileList(tProjectApproval.getListFile(),
                tProjectApproval.getProjectPoolId(),
                tProjectApproval.getId(),
                String.valueOf(FileType.APPROVAL.ordinal()),
                getNickName());

        return toAjax(tProjectApprovalService.updateTProjectApproval(tProjectApproval));
    }

    /**
     * 删除立项申请
     */
    @ApiOperation("删除立项申请")
    @PreAuthorize("@ss.hasPermi('invest:approval:remove')")
    @Log(title = "立项申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tProjectApprovalService.deleteTProjectApprovalByIds(ids));
    }

    /**
     * 根据项目ID获取最新立项申请
     */
    @ApiOperation("根据项目ID获取最新立项申请")
    @GetMapping(value = "/listProjectPoolIdNew")
    public AjaxResult listProjectPoolIdNew(String projectPoolId)
    {
        return success(tProjectApprovalService.listProjectPoolIdNew(projectPoolId));
    }

}
