package com.fs.hisStore.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.domain.model.LoginUser;
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.crm.domain.CrmCustomer;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.framework.web.service.TokenService;
import com.fs.hisStore.domain.FsStoreOrderOfflineScrm;
import com.fs.hisStore.domain.FsStoreOrderOfflineItemScrm;
import com.fs.hisStore.param.FsStoreOrderOfflineParam;
import com.fs.hisStore.service.IFsStoreOrderOfflineItemScrmService;
import com.fs.hisStore.service.IFsStoreOrderOfflineScrmService;
import com.fs.hisStore.vo.FsStoreOrderOfflineListVO;
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 2024-10-29
 */
@RestController
@RequestMapping("/store/store/storeOrderOffline")
public class FsStoreOrderOfflineScrmController extends BaseController
{
    @Autowired
    private IFsStoreOrderOfflineScrmService fsStoreOrderOfflineService;
    @Autowired
    private IFsStoreOrderOfflineItemScrmService fsStoreOrderOfflineItemService;
    @Autowired
    private ICrmCustomerService crmCustomerService;
    @Autowired
    private TokenService tokenService;



    /**
     * 查询线下订单列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreOrderOfflineParam param)
    {
        startPage();
        List<FsStoreOrderOfflineListVO> list = fsStoreOrderOfflineService.selectFsStoreOrderOfflineListVO(param);
        return getDataTable(list);
    }

    /**
     * 导出线下订单列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:export')")
    @Log(title = "线下订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreOrderOfflineScrm fsStoreOrderOffline)
    {
        List<FsStoreOrderOfflineScrm> list = fsStoreOrderOfflineService.selectFsStoreOrderOfflineList(fsStoreOrderOffline);
        ExcelUtil<FsStoreOrderOfflineScrm> util = new ExcelUtil<FsStoreOrderOfflineScrm>(FsStoreOrderOfflineScrm.class);
        return util.exportExcel(list, "storeOrderOffline");
    }

    /**
     * 获取线下订单详细信息
     */
    /**
     * 获取线下订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:query')")
    @GetMapping(value = "/{orderId}")
    public R getInfo(@PathVariable("orderId") Long orderId)
    {

        FsStoreOrderOfflineListVO order=fsStoreOrderOfflineService.selectFsStoreOrderOfflineVOById(orderId);
        order.setMobile(ParseUtils.parsePhone(order.getMobile()));
        CrmCustomer customer=null;
        if(order.getCrmId()!=null&&order.getCrmId()>0){
            customer=crmCustomerService.selectCrmCustomerById(order.getCrmId());
            customer.setMobile(ParseUtils.parsePhone(customer.getMobile()));
        }
        FsStoreOrderOfflineItemScrm itemMap=new FsStoreOrderOfflineItemScrm();
        itemMap.setOrderId(order.getOrderId());
        List<FsStoreOrderOfflineItemScrm> items=fsStoreOrderOfflineItemService.selectFsStoreOrderOfflineItemList(itemMap);
        return R.ok().put("order", order).put("items", items).put("customer",customer) ;
    }

    @GetMapping(value = "/queryPhone/{orderId}")
    @Log(title = "查看电话", businessType = BusinessType.GRANT)
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:queryPhone')")
    public R getPhone(@PathVariable("orderId") Long orderId)
    {
        FsStoreOrderOfflineListVO order=fsStoreOrderOfflineService.selectFsStoreOrderOfflineVOById(orderId);
        String userPhone = order.getMobile();
        return R.ok().put("userPhone",userPhone);
    }

    /**
     * 新增线下订单
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:add')")
    @Log(title = "线下订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreOrderOfflineScrm fsStoreOrderOffline)
    {
        return toAjax(fsStoreOrderOfflineService.insertFsStoreOrderOffline(fsStoreOrderOffline));
    }

    /**
     * 修改线下订单
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:edit')")
    @Log(title = "线下订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreOrderOfflineScrm fsStoreOrderOffline)
    {
        return toAjax(fsStoreOrderOfflineService.updateFsStoreOrderOffline(fsStoreOrderOffline));
    }

    /**
     * 删除线下订单
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:remove')")
    @Log(title = "线下订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(fsStoreOrderOfflineService.deleteFsStoreOrderOfflineByIds(orderIds));
    }


    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:audit')")
    @Log(title = "审核订单", businessType = BusinessType.UPDATE)
    @PostMapping("/auditOrder/{orderId}")
    public R auditOrder(@PathVariable Long orderId)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        FsStoreOrderOfflineScrm fsStoreOrderOffline = new FsStoreOrderOfflineScrm();
        fsStoreOrderOffline.setOrderId(orderId);
        fsStoreOrderOffline.setStatus(3L);
        fsStoreOrderOffline.setAuditBy(loginUser.getUser().getNickName());
        fsStoreOrderOffline.setAuditTime(new Date());
        if (fsStoreOrderOfflineService.updateFsStoreOrderOffline(fsStoreOrderOffline)>0){
            return R.ok();
        }
        return R.error("审核失败");
    }
}
