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.TProjectDecision;
import com.ruoyi.invest.service.ITProjectDecisionService;
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/decision")
public class TProjectDecisionController extends BaseController
{
    @Autowired
    private ITProjectDecisionService tProjectDecisionService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ITProjectPoolService tProjectPoolService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    /**
     * 查询投决申请列表
     */
    @ApiOperation("查询投决申请列表")
    @PreAuthorize("@ss.hasPermi('invest:decision:list')")
    @GetMapping("/list")
    public TableDataInfo list(TProjectDecision tProjectDecision)
    {
        startPage();
        List<TProjectDecision> list = tProjectDecisionService.selectTProjectDecisionList(tProjectDecision);
        return getDataTable(list);
    }

    /**
     * 导出投决申请列表
     */
    @ApiOperation("导出投决申请列表")
    @PreAuthorize("@ss.hasPermi('invest:decision:export')")
    @Log(title = "投决申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectDecision tProjectDecision)
    {
        List<TProjectDecision> list = tProjectDecisionService.selectTProjectDecisionList(tProjectDecision);
        ExcelUtil<TProjectDecision> util = new ExcelUtil<TProjectDecision>(TProjectDecision.class);
        util.exportExcel(response, list, "投决申请数据");
    }

    /**
     * 获取投决申请详细信息
     */
    @ApiOperation("获取投决申请详细信息")
    @PreAuthorize("@ss.hasPermi('invest:decision:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectDecisionService.selectTProjectDecisionById(id));
    }

    /**
     * 新增投决申请
     */
    @ApiOperation("新增投决申请")
    @Log(title = "投决申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectDecision tProjectDecision)
    {
        tProjectDecision.setId(IdUtils.fastSimpleUUID());
        // todo 保存投决资料
        tUnifyFileService.insertTUnifyFileList(tProjectDecision.getListFile(),
                tProjectDecision.getProjectPoolId(),
                tProjectDecision.getId(),
                String.valueOf(FileType.DECISION.ordinal()),
                getNickName());

        // todo 更改投决状态
        tProjectPoolService.updateTProjectPoolByDecisionFlag(tProjectDecision.getProjectPoolId(),"1");

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

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

        return toAjax(tProjectDecisionService.insertTProjectDecision(tProjectDecision));
    }

    /**
     * 修改投决申请
     */
    @ApiOperation("修改投决申请")
    @PreAuthorize("@ss.hasPermi('invest:decision:edit')")
    @Log(title = "投决申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectDecision tProjectDecision)
    {
        // todo 保存投决资料
        tUnifyFileService.insertTUnifyFileList(tProjectDecision.getListFile(),
                tProjectDecision.getProjectPoolId(),
                tProjectDecision.getId(),
                String.valueOf(FileType.DECISION.ordinal()),
                getNickName());

        return toAjax(tProjectDecisionService.updateTProjectDecision(tProjectDecision));
    }

    /**
     * 删除投决申请
     */
    @ApiOperation("删除投决申请")
    @PreAuthorize("@ss.hasPermi('invest:decision:remove')")
    @Log(title = "投决申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tProjectDecisionService.deleteTProjectDecisionByIds(ids));
    }

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

}
