package com.fs.his.controller;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.SecurityUtils;
import com.fs.his.domain.FsExportTask;
import com.fs.his.mapper.FsPrescribeMapper;
import com.fs.his.param.FsStorePaymentParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.vo.FsStorePaymentExcelVO;
import com.fs.his.vo.FsStorePaymentVO;
import lombok.extern.slf4j.Slf4j;
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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsStorePayment;
import com.fs.his.service.IFsStorePaymentService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 支付明细Controller
 *
 * @author fs
 * @date 2023-08-21
 */
@RestController
@RequestMapping("/his/storePayment")
@Slf4j
public class FsStorePaymentController extends BaseController
{
    @Autowired
    private IFsStorePaymentService fsStorePaymentService;
    @Autowired
    FsPrescribeMapper fsPrescribeMapper;

    @Autowired
    private IFsExportTaskService exportTaskService;
    /**
     * 查询支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStorePaymentParam fsStorePayment)
    {
        startPage();
        List<FsStorePaymentVO> list = fsStorePaymentService.selectFsStorePaymentListVO(fsStorePayment);
        for (FsStorePaymentVO vo : list){
            if (vo.getBusinessType()==1){
                vo.setPrescribeCode(vo.getBusinessCode());
            }else if (vo.getBusinessType()==2){
                String code = fsPrescribeMapper.selectFsPrescribeCodeByOrderId(vo.getBusinessId());
                vo.setPrescribeCode(code);
            }else if (vo.getBusinessType()==3&&vo.getStatus()!=0){
                String code =   fsPrescribeMapper.selectFsPrescribeCodeByOrderCode(vo.getBusinessCode());
                vo.setPrescribeCode(code);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:export')")
    @Log(title = "支付明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStorePaymentParam fsStorePayment)
    {
        Integer exportType1 = exportTaskService.isExportType1(SecurityUtils.getUserId());
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }

        if (fsStorePaymentService.isEntityNull(fsStorePayment)){
            return AjaxResult.error("请筛选数据导出");
        }
        logger.info("hz>\n【支付明细】：{}", SecurityUtils.getUserId());
        Long count = fsStorePaymentService.selectFsStorePaymentExcelVOCount(fsStorePayment);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        FsExportTask task=new FsExportTask();
        task.setTaskType(10);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("支付明细导出");
        task.setSysType(1);
        task.setUserId(SecurityUtils.getUserId());
        exportTaskService.insertFsExportTask(task);
        fsStorePayment.setTaskId(task.getTaskId());
        exportTaskService.exportStorePaymentData(fsStorePayment);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());

    }


    /**
     * 获取支付明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:query')")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        FsStorePaymentVO fsStorePaymentVO = fsStorePaymentService.selectFsStorePaymentByPaymentIdVO(paymentId);
        if (fsStorePaymentVO.getPhone()!=null&&!fsStorePaymentVO.getPhone().equals("")){
            fsStorePaymentVO.setPhone(fsStorePaymentVO.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(fsStorePaymentVO);
    }


    @GetMapping(value = "getStatus/{paymentId}")
    public AjaxResult getStatus(@PathVariable("paymentId") Long paymentId)
    {

        return AjaxResult.success( fsStorePaymentService.updateFsStorePaymentByDecryptForm(paymentId));
    }
    @PreAuthorize("@ss.hasPermi('his:storePayment:refund')")
    @GetMapping(value = "refund/{paymentId}")
    public R refund(@PathVariable("paymentId") Long paymentId)
    {
        log.info("sysUserId： {} 提交退款", getUserId());
        return fsStorePaymentService.refundFsStorePayment(paymentId);
    }

    /**
     * 新增支付明细
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:add')")
    @Log(title = "支付明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStorePayment fsStorePayment)
    {
        return toAjax(fsStorePaymentService.insertFsStorePayment(fsStorePayment));
    }

    /**
     * 修改支付明细
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:edit')")
    @Log(title = "支付明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStorePayment fsStorePayment)
    {
        return toAjax(fsStorePaymentService.updateFsStorePayment(fsStorePayment));
    }

    /**
     * 删除支付明细
     */
    @PreAuthorize("@ss.hasPermi('his:storePayment:remove')")
    @Log(title = "支付明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(fsStorePaymentService.deleteFsStorePaymentByPaymentIds(paymentIds));
    }
}
