package com.staffing.web.controller.custom;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.service.IStTaskSignService;
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.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.StEmployeeFundRecord;
import com.staffing.custom.service.IStEmployeeFundRecordService;
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("/fund/fund")
public class StEmployeeFundRecordController extends BaseController
{
    @Autowired
    private IStEmployeeFundRecordService stEmployeeFundRecordService;

    @Autowired
    private IStTaskSignService taskSignService;

    /**
     * 查询员工资金流水列表
     */
    @PreAuthorize("@ss.hasPermi('fund:list')")
    @GetMapping("/list")
    public TableDataInfo list(StEmployeeFundRecord stEmployeeFundRecord)
    {
        startPage();
        List<StEmployeeFundRecord> list = stEmployeeFundRecordService.selectStEmployeeFundRecordList(stEmployeeFundRecord);
        return getDataTable(list);
    }

    /**
     * 导出员工资金流水列表
     */
    @PreAuthorize("@ss.hasPermi('fund:export')")
    @Log(title = "员工资金流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StEmployeeFundRecord stEmployeeFundRecord)
    {
        List<StEmployeeFundRecord> list = stEmployeeFundRecordService.selectStEmployeeFundRecordList(stEmployeeFundRecord);
        List<StEmployeeFundRecord> export = new ArrayList<>();
        for (StEmployeeFundRecord employeeFundRecord : list) {
            if (employeeFundRecord.getPayStatus() == 0){
                employeeFundRecord.setPayStatusStr("未发");
            }else if (employeeFundRecord.getPayStatus() == 1){
                employeeFundRecord.setPayStatusStr("已发");
                continue;
            }else if (employeeFundRecord.getPayStatus() == 2){
                employeeFundRecord.setPayStatusStr("发放失败");
            }
            if (employeeFundRecord.getPayWay() != null){
                employeeFundRecord.setPayWayStr("微信转账");
            }
            if (employeeFundRecord.getStTaskSign() != null){
                employeeFundRecord.setRemark(employeeFundRecord.getStTaskSign().getRemark());
            }
            export.add(employeeFundRecord);
        }
        ExcelUtil<StEmployeeFundRecord> util = new ExcelUtil<StEmployeeFundRecord>(StEmployeeFundRecord.class);

        util.exportExcel(response, export, "员工资金流水数据");
    }

    /**
     * 获取员工资金流水详细信息
     */
    @PreAuthorize("@ss.hasPermi('fund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stEmployeeFundRecordService.selectStEmployeeFundRecordById(id));
    }

    /**
     * 新增员工资金流水
     */
    @PreAuthorize("@ss.hasPermi('fund:add')")
    @Log(title = "员工资金流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StEmployeeFundRecord stEmployeeFundRecord)
    {
        stEmployeeFundRecord.setUpdateTime(new Date());
        return toAjax(stEmployeeFundRecordService.insertStEmployeeFundRecord(stEmployeeFundRecord));
    }

    /**
     * 修改员工资金流水
     */
    @PreAuthorize("@ss.hasPermi('fund:edit')")
    @Log(title = "员工资金流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StEmployeeFundRecord stEmployeeFundRecord)
    {
        stEmployeeFundRecord.setUpdateTime(new Date());
        return toAjax(stEmployeeFundRecordService.updateStEmployeeFundRecord(stEmployeeFundRecord));
    }

    /**
     * 删除员工资金流水
     */
    @PreAuthorize("@ss.hasPermi('fund:remove')")
    @Log(title = "员工资金流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stEmployeeFundRecordService.deleteStEmployeeFundRecordByIds(ids));
    }

    @Log(title = "流水导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('fund:export')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<StEmployeeFundRecord> util = new ExcelUtil<>(StEmployeeFundRecord.class);
        List<StEmployeeFundRecord> records = util.importExcel(file.getInputStream());

        String message = stEmployeeFundRecordService.importRecord(records);
        return success(message);
    }

}
