package com.fs.company.controller.store;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.StringUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsExportTask;
import com.fs.his.domain.FsStoreAfterSales;
import com.fs.his.param.FsStoreAfterSalesParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.service.IFsStoreAfterSalesService;
import com.fs.his.vo.FsStoreAfterSalesListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 售后记录Controller
 *
 * @author fs
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/store/storeAfterSales")
public class FsStoreAfterSalesController extends BaseController
{
    @Autowired
    private IFsStoreAfterSalesService fsStoreAfterSalesService;
    @Autowired
    private IFsExportTaskService exportTaskService;
    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreAfterSalesParam fsStoreAfterSales)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        if(!StringUtils.isEmpty(fsStoreAfterSales.getCreateTimeRange())){
            fsStoreAfterSales.setCreateTimeList(fsStoreAfterSales.getCreateTimeRange().split("--"));
        }
        List<FsStoreAfterSalesListVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        return getDataTable(list);
    }

    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsStoreAfterSalesParam fsStoreAfterSales)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        fsStoreAfterSales.setCompanyUserId(loginUser.getUser().getUserId());
        if(!StringUtils.isEmpty(fsStoreAfterSales.getCreateTimeRange())){
            fsStoreAfterSales.setCreateTimeList(fsStoreAfterSales.getCreateTimeRange().split("--"));
        }
        List<FsStoreAfterSalesListVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        return getDataTable(list);
    }



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

        Long count =fsStoreAfterSalesService.selectFsStoreAfterSalesExcelListVOCount(fsStoreAfterSales);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        FsExportTask task=new FsExportTask();
        task.setTaskType(7);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("药品订单导出");
        task.setSysType(2);
        task.setCompanyUserId(userId);
        exportTaskService.insertFsExportTask(task);
        fsStoreAfterSales.setTaskId(task.getTaskId());
        fsStoreAfterSalesService.exportData(fsStoreAfterSales);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());
    }

    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:myExport')")
    @Log(title = "售后记录", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsStoreAfterSalesParam fsStoreAfterSales)
    {
        if(!StringUtils.isEmpty(fsStoreAfterSales.getCreateTimeRange())){
            fsStoreAfterSales.setCreateTimeList(fsStoreAfterSales.getCreateTimeRange().split("--"));
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        logger.info("tc>\n【导出售后记录】："+ userId);
        if (fsStoreAfterSalesService.isEntityNull(fsStoreAfterSales)){
            return AjaxResult.error("请筛选数据导出");
        }
        Integer exportType1 = exportTaskService.isExportType2(userId);
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        fsStoreAfterSales.setCompanyUserId(loginUser.getUser().getUserId());
        Long count =fsStoreAfterSalesService.selectFsStoreAfterSalesExcelListVOCount(fsStoreAfterSales);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }

        FsExportTask task=new FsExportTask();
        task.setTaskType(7);
        task.setStatus(0);
        task.setStartTime(new Date());
        task.setRemark("药品订单导出");
        task.setSysType(2);
        task.setCompanyUserId(userId);
        exportTaskService.insertFsExportTask(task);
        fsStoreAfterSales.setTaskId(task.getTaskId());
        fsStoreAfterSalesService.exportData(fsStoreAfterSales);
        return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());
    }



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

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

    /**
     * 修改售后记录
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:edit')")
    @Log(title = "售后记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreAfterSales fsStoreAfterSales)
    {
        fsStoreAfterSales.setStatus(2);
        return toAjax(fsStoreAfterSalesService.updateFsStoreAfterSales(fsStoreAfterSales));
    }

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

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

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

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

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


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

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


}
