package com.staffing.web.controller.custom;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.staffing.common.utils.MinioUtil;
import com.staffing.common.utils.QRCodeGenerator;
import com.staffing.custom.domain.StTaskSign;
import io.minio.ObjectWriteResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.staffing.common.annotation.Log;
import com.staffing.common.core.controller.BaseController;
import com.staffing.common.core.domain.AjaxResult;
import com.staffing.common.enums.BusinessType;
import com.staffing.custom.domain.StTask;
import com.staffing.custom.service.IStTaskService;
import com.staffing.common.utils.poi.ExcelUtil;
import com.staffing.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 任务Controller
 * 
 * @author staffing
 * @date 2024-08-27
 */
@RestController
@RequestMapping("/task/task")
public class StTaskController extends BaseController
{
    @Autowired
    private IStTaskService stTaskService;

    @Autowired
    private MinioUtil minioUtil;
    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:list')")
    @GetMapping("/list")
    public TableDataInfo list(StTask stTask)
    {
        startPage();
        List<StTask> list = stTaskService.selectStTaskList(stTask, 0);
        for (StTask task : list) {
            if (task.getCodePic() == null){
                continue;
            }

            task.setCodePic("https://www.tulkj.cn/file/yonggong/" + task.getCodePic());
//            task.setCodePic(minioUtil.getObjectURL( "yonggong", task.getCodePic()));
        }
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StTask stTask)
    {
        List<StTask> list = stTaskService.selectStTaskList(stTask, 0);
        ExcelUtil<StTask> util = new ExcelUtil<StTask>(StTask.class);
        util.exportExcel(response, list, "任务数据");
    }



    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stTaskService.selectStTaskById(id));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StTask stTask)
    {
        stTask.setStatus(0);
        stTask.setCreateTime(new Date());
        stTask.setUpdateTime(new Date());
        stTaskService.insertStTask(stTask);

        String url = "http://www.tulkj.cn:81/index.html?id=" + stTask.getId();
        InputStream inputStream = QRCodeGenerator.generateQRCodeImage(url, 500, 500);
        String fileName = System.currentTimeMillis() + stTask.getId() + ".png";
        try {
            ObjectWriteResponse objectWriteResponse = minioUtil.putObject("yonggong"
                    , fileName, inputStream, inputStream.available(), "image/png");
        } catch (Exception e) {
            logger.error("minioUtil.putObject error", e);
        }

        stTask.setCodePic(fileName);

        return toAjax(stTaskService.updateStTask(stTask));
    }



    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StTask stTask)
    {
        //已发放金额的任务不许删除
        StTask stTask1 = stTaskService.selectStTaskById(stTask.getId(), true);
        for (StTaskSign stTaskSign : stTask1.getListSign()) {
            if (stTaskSign.getStEmployeeFundRecord().getPayStatus() == 1){
                return error("已发薪的任务禁止修改");
            }
        }

        stTask.setUpdateTime(new Date());
        return toAjax(stTaskService.updateStTask(stTask));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping("shangjia")
    public AjaxResult shangjia(@RequestBody StTask stTask)
    {
        stTask.setUpdateTime(new Date());
        return toAjax(stTaskService.updateStTask(stTask, false));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //已发放金额的任务不许删除
        for (Long id : ids) {
            StTask stTask = stTaskService.selectStTaskById(id, true);
            for (StTaskSign stTaskSign : stTask.getListSign()) {
                if (stTaskSign.getStEmployeeFundRecord().getPayStatus() == 1){
                    return error("已发薪的任务禁止删除");
                }
            }
        }

        return toAjax(stTaskService.deleteStTaskByIds(ids));
    }
}
