package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.SysCustomer;
import com.yunfeiyun.agmp.fms.domain.reqvo.SysCustomerEditReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.SysCustomerResVo;
import com.yunfeiyun.agmp.fms.service.ISysCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户Controller
 * 
 * @author 杨晓辉
 * @date 2023-07-06
 */
@RestController
@RequestMapping("/sys/customer")
public class SysCustomerController extends BaseController
{
    @Autowired
    private ISysCustomerService sysCustomerService;
    @Resource
    private ResManager resManager;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCustomer sysCustomer)
    {
        startPage();
        List<SysCustomerResVo> list = sysCustomerService.selectSysCustomerList(sysCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCustomer sysCustomer)
    {
        List<SysCustomerResVo> list = sysCustomerService.selectSysCustomerList(sysCustomer);
        ExcelUtil<SysCustomerResVo> util = new ExcelUtil<SysCustomerResVo>(SysCustomerResVo.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:info')")
    @GetMapping(value = "/info/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") String customerId)
    {
        return success(sysCustomerService.selectSysCustomerByCustomerId(customerId));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysCustomerEditReqVo sysCustomerEditReqVo)
    {
        sysCustomerEditReqVo.setCustomerId(sysCustomerEditReqVo.getId());
        resManager.updateRes(sysCustomerEditReqVo.getResId(),sysCustomerEditReqVo.getCustomerId());
        return toAjax(sysCustomerService.insertSysCustomer(sysCustomerEditReqVo));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysCustomerEditReqVo sysCustomerEditReqVo)
    {
        resManager.updateRes(sysCustomerEditReqVo.getResId(),sysCustomerEditReqVo.getCustomerId());
        return toAjax(sysCustomerService.updateSysCustomer(sysCustomerEditReqVo));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:delete')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(String id)
    {
        return toAjax(sysCustomerService.deleteSysCustomerByCustomerId(id));
    }

    /**
     * 体检报告图片上传
     */
    @PreAuthorize("@ss.hasPermi('sys:customer:quality:upload')")
    @Log(title = "体检报告图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/quality/upload")
    public AjaxResult qualityUpload(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.CUSTOMER_QUALIFICATION, ResConstants.mediaType.IMG, "", file,getLoginUser());
            return success(sysRes);
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }
}
