package com.fs.store.controller;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.store.domain.*;
import com.fs.store.service.*;
import com.fs.store.vo.FsStoreAfterSalesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 售后记录Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/storeAfterSales")
public class FsStoreAfterSalesController extends BaseController
{
    @Autowired
    private IFsStoreOrderService fsStoreOrderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private IFsStoreAfterSalesService fsStoreAfterSalesService;
    @Autowired
    private IFsStoreAfterSalesItemService fsStoreAfterSalesItemService;
    @Autowired
    private IFsStoreAfterSalesStatusService storeAfterSalesStatusService;
    @Autowired
    private ICompanyUserService companyUserService;
    @Autowired
    private ICompanyService companyService;


    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        startPage();
        List<FsStoreAfterSalesVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        for (FsStoreAfterSalesVO vo : list){
            vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
        }
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsStoreAfterSales fsStoreAfterSales)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        fsStoreAfterSales.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<FsStoreAfterSalesVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        for (FsStoreAfterSalesVO vo : list){
            vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
        }
        return getDataTable(list);
    }


    /**
     * 导出售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:export')")
    @Log(title = "售后记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreAfterSales fsStoreAfterSales)
    {
        if (fsStoreAfterSales.getBeginTime().equals("") && fsStoreAfterSales.getEndTime().equals("")){
            fsStoreAfterSales.setBeginTime(null);
            fsStoreAfterSales.setEndTime(null);
        }
        if (fsStoreAfterSalesService.isEntityNull(fsStoreAfterSales)){
            return AjaxResult.error("请筛选数据导出");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreAfterSales.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsStoreAfterSalesVO> list = fsStoreAfterSalesService.selectFsStoreAfterSalesListVO(fsStoreAfterSales);
        for (FsStoreAfterSalesVO vo : list){
            vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
        }
        ExcelUtil<FsStoreAfterSalesVO> util = new ExcelUtil<FsStoreAfterSalesVO>(FsStoreAfterSalesVO.class);
        return util.exportExcel(list, "storeAfterSales");
    }

    /**
     * 获取售后记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        FsStoreAfterSales afterSales=fsStoreAfterSalesService.selectFsStoreAfterSalesById(id);
        FsStoreAfterSalesItem map=new FsStoreAfterSalesItem();
        map.setStoreAfterSalesId(id);
        List<FsStoreAfterSalesItem> items=fsStoreAfterSalesItemService.selectFsStoreAfterSalesItemList(map);
        FsStoreAfterSalesStatus statusMap=new FsStoreAfterSalesStatus();
        statusMap.setStoreAfterSalesId(id);
        List<FsStoreAfterSalesStatus> logs=storeAfterSalesStatusService.selectFsStoreAfterSalesStatusList(statusMap);
        FsUser user=userService.selectFsUserById(afterSales.getUserId());
        user.setPhone(ParseUtils.parsePhone(user.getPhone()));
        FsStoreOrder order=fsStoreOrderService.selectFsStoreOrderByOrderCode(afterSales.getOrderCode());
        return R.ok().put("afterSales",afterSales).put("items",items).put("logs",logs).put("user",user).put("order",order);
    }

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





}
