package com.fs.his.controller;

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

import cn.hutool.core.date.DateTime;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.framework.web.service.TokenService;
import com.fs.his.domain.FsExportTask;
import com.fs.his.domain.FsStoreAfterSalesLogs;
import com.fs.his.domain.FsUser;
import com.fs.his.enums.FsStoreAfterSalesStatusEnum;
import com.fs.his.param.FsStoreAfterSalesParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.service.IFsStoreAfterSalesLogsService;
import com.fs.his.vo.FsStoreAfterSalesExcelVO;
import com.fs.his.vo.FsStoreAfterSalesListVO;
import com.fs.his.vo.FsStoreAfterSalesVO;
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.FsStoreAfterSales;
import com.fs.his.service.IFsStoreAfterSalesService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 售后记录Controller
 *
 * @author fs
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/his/storeAfterSales")
public class FsStoreAfterSalesController extends BaseController
{
    @Autowired
    private IFsStoreAfterSalesService fsStoreAfterSalesService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IFsStoreAfterSalesLogsService fsStoreAfterSalesLogsService;
    @Autowired
    private IFsExportTaskService exportTaskService;
    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreAfterSalesParam fsStoreAfterSales)
    {
        startPage();
        if(!StringUtils.isEmpty(fsStoreAfterSales.getCreateTimeRange())){
            fsStoreAfterSales.setCreateTimeList(fsStoreAfterSales.getCreateTimeRange().split("--"));
        }
        List<FsStoreAfterSalesListVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        return getDataTable(list);
    }

    /**
     * 导出售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:export')")
    @Log(title = "售后记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreAfterSalesParam fsStoreAfterSales)
    {
        if (fsStoreAfterSalesService.isEntityNull(fsStoreAfterSales)){
            return AjaxResult.error("请筛选数据导出");
        }
        Integer exportType1 = exportTaskService.isExportType1(SecurityUtils.getUserId());
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }
        if(!StringUtils.isEmpty(fsStoreAfterSales.getCreateTimeRange())){
            fsStoreAfterSales.setCreateTimeList(fsStoreAfterSales.getCreateTimeRange().split("--"));
        }
        logger.info("tc>\n【售后记录导出】：{}", SecurityUtils.getUserId());
        List<FsStoreAfterSalesExcelVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesExcelListVO(fsStoreAfterSales);

        FsExportTask task=new FsExportTask();
        task.setTaskType(11);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("售后记录导出");
        task.setSysType(1);
        task.setUserId(SecurityUtils.getUserId());
        exportTaskService.insertFsExportTask(task);
        fsStoreAfterSales.setTaskId(task.getTaskId());
        fsStoreAfterSalesService.exportData(fsStoreAfterSales);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());

    }

    /**
     * 获取售后记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsStoreAfterSalesService.selectFsStoreAfterSalesVOById(id));
    }

    /**
     * 新增售后记录
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:add')")
    @Log(title = "售后记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        return toAjax(fsStoreAfterSalesService.insertFsStoreAfterSales(fsStoreAfterSales));
    }


    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:edit')")
    @Log(title = "编辑退回物流", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        fsStoreAfterSales.setStatus(2);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //操作记录
        FsStoreAfterSalesLogs logs = new FsStoreAfterSalesLogs();
        logs.setChangeTime(new DateTime());
        logs.setChangeType(2);
        logs.setOperator(loginUser.getUser().getNickName());
        logs.setStoreAfterSalesId(fsStoreAfterSales.getId());
        logs.setChangeMessage(FsStoreAfterSalesStatusEnum.STATUS_2.getDesc());
        fsStoreAfterSalesLogsService.insertFsStoreAfterSalesLogs(logs);
        return toAjax(fsStoreAfterSalesService.updateFsStoreAfterSales(fsStoreAfterSales));
    }

    /**
     * 删除售后记录
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:remove')")
    @Log(title = "售后记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsStoreAfterSalesService.deleteFsStoreAfterSalesByIds(ids));
    }

    /**
     * 获取售后记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:query')")
    @GetMapping(value = "itemList/{id}")
    public AjaxResult getItemList(@PathVariable("id") Long id)
    {

        return AjaxResult.success(fsStoreAfterSalesService.selectFsStoreAfterSalesItemById(id));
    }

    /**
     * 获取售后记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:query')")
    @GetMapping(value = "logList/{id}")
    public AjaxResult getLogList(@PathVariable("id") Long id)
    {

        return AjaxResult.success(fsStoreAfterSalesService.selectFsStoreAfterSalesLogById(id));
    }



    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:query')")
    @GetMapping(value = "codeId/{id}")
    public AjaxResult getcodeId(@PathVariable("id") Long id)
    {

        Long s = fsStoreAfterSalesService.selectFsOrderIdByCode(id);
        return AjaxResult.success(s);
    }


    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:audit')")
    @PutMapping("/auditing")
    @Log(title = "平台审核", businessType = BusinessType.UPDATE)
    public AjaxResult auditing(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setOperator(loginUser.getUser().getNickName());
        return toAjax(fsStoreAfterSalesService.auditing(fsStoreAfterSales));
    }

    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:depot')")
    @Log(title = "仓库审核", businessType = BusinessType.UPDATE)
    @PutMapping("/depotAuditing")
    public AjaxResult depotAuditing(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setOperator(loginUser.getUser().getNickName());
        return toAjax(fsStoreAfterSalesService.depotAuditing(fsStoreAfterSales));
    }
    /**
     * 审核
     */
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:revoke')")
    @PutMapping("/noAuditing")
    public AjaxResult noAuditing(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setOperator(loginUser.getUser().getNickName());
        return toAjax(fsStoreAfterSalesService.noAuditing(fsStoreAfterSales));
    }

    /**
     * 财务审核并退款
     */
    @Log(title = "财务审核", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('his:storeAfterSales:finance')")
    @PutMapping("/tuiMoney")
    public AjaxResult tuiMoney(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setOperator(loginUser.getUser().getNickName());
        return toAjax(fsStoreAfterSalesService.refundMoney(fsStoreAfterSales));
    }

}
