package com.fs.store.controller;

import java.text.ParseException;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.express.FsStoreDeliversService;
import com.fs.express.cache.FsStoreDeliversCacheService;
import com.fs.store.domain.*;

import com.fs.store.param.FsStoreAfterSalesAudit1Param;
import com.fs.store.param.FsStoreAfterSalesAudit2Param;
import com.fs.store.param.FsStoreAfterSalesCancelParam;
import com.fs.store.param.FsStoreAfterSalesRefundParam;
import com.fs.store.service.*;
import com.fs.store.vo.FsStoreAfterSalesVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

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

    @Autowired
    private TokenService tokenService;

    @Autowired
    private FsStoreDeliversCacheService fsStoreDeliversCacheService;

    @Autowired
    private FsStoreDeliversService fsStoreDeliversService;
    /**
     * 查询售后记录列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreAfterSales fsStoreAfterSales)
    {
        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);
        }
        Boolean a = fsStoreAfterSalesService.isEntityNull(fsStoreAfterSales);
        if (fsStoreAfterSalesService.isEntityNull(fsStoreAfterSales)){
            return AjaxResult.error("请筛选数据导出");
        }
        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());

        if(order != null) {
            afterSales.setPhoneNumber(order.getUserPhone());
            afterSales.setAddress(order.getUserAddress());
            afterSales.setConsignee(order.getRealName());
        }
        List<FsStoreDelivers> byOrderId = fsStoreDeliversService.findByOrderId(order.getId());
        if(CollectionUtils.isNotEmpty(byOrderId)){
            FsStoreDelivers delivers = byOrderId.get(0);
            afterSales.setDeliveryName(delivers.getDeliverName());
            afterSales.setDeliverySn(delivers.getDeliverSn());
            afterSales.setDeliveryId(delivers.getDeliverId());
            afterSales.setDeliveryStatus(delivers.getStatus());
        }
        return R.ok().put("afterSales",afterSales).put("items",items).put("logs",logs).put("user",user).put("order",order);
    }

    /**
     * 新增售后记录
     */
    @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)
    {
        if ((!"".equals(fsStoreAfterSales.getDeliveryName()) && !"".equals(fsStoreAfterSales.getDeliverySn())) || (fsStoreAfterSales.getDeliveryName() == null && fsStoreAfterSales.getDeliverySn() == null)){
            fsStoreAfterSales.setStatus(2);
        }
        return toAjax(fsStoreAfterSalesService.updateDelivery(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:audit1')")
    @PostMapping("/audit1")
    //平台审核
    public R audit1(@RequestBody FsStoreAfterSalesAudit1Param param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return fsStoreAfterSalesService.audit1(param);
    }

    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:audit2')")
    @PostMapping("/audit2")
    //仓库审核
    public R audit2(@RequestBody FsStoreAfterSalesAudit2Param param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return fsStoreAfterSalesService.audit2(param);
    }
    //财务审核
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:refund')")
    @PostMapping("/refund")
    public R refund(@RequestBody FsStoreAfterSalesRefundParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return fsStoreAfterSalesService.refundMoney(param);
    }
    //平台撤销
    @PreAuthorize("@ss.hasPermi('store:storeAfterSales:cancel')")
    @PostMapping("/cancel")
    public R cancel(@RequestBody FsStoreAfterSalesCancelParam param) throws ParseException {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setOperator(loginUser.getUser().getNickName());
        return fsStoreAfterSalesService.cancel(param);
    }

    @GetMapping("/getStoreAfterSales")
    public R getStoreAfterSales(@RequestParam("orderCode") String orderCode)
    {
        List<FsStoreAfterSalesVO> vo = fsStoreAfterSalesService.selectFsStoreAfterSalesVOByOrderCode(orderCode);
        return R.ok().put("data",vo);
    }

}
