package com.fs.store.controller;

import java.util.Date;
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.crm.domain.CrmCustomer;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.store.domain.FsStoreOrder;
import com.fs.store.domain.FsStoreOrderOfflineItem;
import com.fs.store.param.FsStoreOrderOfflineParam;
import com.fs.store.service.IFsStoreOrderOfflineItemService;
import com.fs.store.vo.FsStoreOrderOfflineListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.store.domain.FsStoreOrderOffline;
import com.fs.store.service.IFsStoreOrderOfflineService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 线下订单Controller
 *
 * @author fs
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/store/storeOrderOffline")
public class FsStoreOrderOfflineController extends BaseController
{
    @Autowired
    private IFsStoreOrderOfflineService fsStoreOrderOfflineService;
    @Autowired
    private IFsStoreOrderOfflineItemService 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(FsStoreOrderOffline fsStoreOrderOffline)
    {
        List<FsStoreOrderOffline> list = fsStoreOrderOfflineService.selectFsStoreOrderOfflineList(fsStoreOrderOffline);
        ExcelUtil<FsStoreOrderOffline> util = new ExcelUtil<FsStoreOrderOffline>(FsStoreOrderOffline.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()));
        }
        FsStoreOrderOfflineItem itemMap=new FsStoreOrderOfflineItem();
        itemMap.setOrderId(order.getOrderId());
        List<FsStoreOrderOfflineItem> 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 FsStoreOrderOffline fsStoreOrderOffline)
    {
        return toAjax(fsStoreOrderOfflineService.insertFsStoreOrderOffline(fsStoreOrderOffline));
    }

    /**
     * 修改线下订单
     */
    @PreAuthorize("@ss.hasPermi('store:storeOrderOffline:edit')")
    @Log(title = "线下订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreOrderOffline 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());
        FsStoreOrderOffline fsStoreOrderOffline = new FsStoreOrderOffline();
        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("审核失败");
    }
}
