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.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.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hisStore.domain.*;
import com.fs.hisStore.param.FsStoreOrderOfflineCreateParam;
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.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 TokenService tokenService;
    @Autowired
    private ICrmCustomerService crmCustomerService;

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

    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsStoreOrderOfflineParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        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}")
    @PreAuthorize("@ss.hasPermi('store:storeOrder:queryPhone')")
    @Log(title = "查看电话", businessType = BusinessType.GRANT)
    public R getPhone(@PathVariable("orderId") Long orderId)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getUserType().equals("00")){
            FsStoreOrderOfflineListVO order=fsStoreOrderOfflineService.selectFsStoreOrderOfflineVOById(orderId);
            String userPhone = order.getMobile();
            return R.ok().put("userPhone",userPhone);
        }
        return R.error("无权查看");
    }

    /**
     * 新增线下订单
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:add')")
    @Log(title = "线下订单", businessType = BusinessType.INSERT)
    @PostMapping("/createOrder")
    public R add(@RequestBody FsStoreOrderOfflineCreateParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        return fsStoreOrderOfflineService.createOrder(param);
    }

    /**
     * 修改线下订单
     */
    @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));
    }

    @Log(title = "订单凭证上传", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:uploadCredentials')")
    @PostMapping("/uploadCredentials")
    public R UploadCredentials(@RequestBody FsStoreOrderOfflineScrm order)
    {
        if (order.getCertificates() == null ){
            return R.error("请选择图片上传");
        }
        fsStoreOrderOfflineService.uploadCredentials(order);
        return R.ok();
    }
}
