package com.ruoyi.web.controller.warehouse.basicData;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.service.ITCorpsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
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 ruoyi
 * @date 2020-12-11
 */
@RestController
@RequestMapping("/basicdata/corps")
public class TCorpsController extends BaseController {
    @Autowired
    private ITCorpsService tCorpsService;

    /**
     * 查询客户详情列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCorps tCorps) {
        startPage();
        List<TCorps> list = tCorpsService.selectTCorpsList(tCorps);
        return getDataTable(list);
    }

    /**
     * 导出客户详情列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:export')")
    @Log(title = "客户详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCorps tCorps) {
        List<TCorps> list = tCorpsService.selectTCorpsList(tCorps);
        ExcelUtil<TCorps> util = new ExcelUtil<TCorps>(TCorps.class);
        return util.exportExcel(list, "corps");
    }

    /**
     * 获取客户详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tCorpsService.selectTCorpsById(fId));
    }

    /**
     * 新增客户详情
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:add')")
    @Log(title = "客户详情", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestParam("corps") String tCorps, @RequestParam("customerContacts") String tCustomerContacts) {
        // 检验编号、名称、简称 唯一
        TCorps tCorps1 = JSONArray.parseObject(tCorps, TCorps.class);
        if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFNoUnique(tCorps1))) {
            return AjaxResult.error("新增客户'" + tCorps1.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFNnameUnique(tCorps1))) {
            return AjaxResult.error("新增客户'" + tCorps1.getfName() + "'失败，名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFCnameUnique(tCorps1))) {
            return AjaxResult.error("新增客户'" + tCorps1.getfCname() + "'失败，简称已存在");
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tCorpsService.insertTCorps(tCorps, tCustomerContacts, loginUser);
    }

    /**
     * 修改客户详情basicdata/customerDetails/corps
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:edit')")
    @Log(title = "客户详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TCorps tCorps) {
        return tCorpsService.updateTCorps(tCorps);
    }

    /**
     * 删除客户详情
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:remove')")
    @Log(title = "客户详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return tCorpsService.deleteTCorpsByIds(fIds);
    }
}
