package com.ruoyi.ibs.list.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.list.domain.CustInfoRetail;
import com.ruoyi.ibs.list.domain.CustManualTagDTO;
import com.ruoyi.ibs.list.domain.CustTagDTO;
import com.ruoyi.ibs.list.domain.TreeNode;
import com.ruoyi.ibs.list.service.*;
import com.ruoyi.system.service.ISysIndustryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 对私客户信息Controller
 *
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/system/retail")
@Api("对私客户信息")
public class CustInfoRetailController extends BaseController {
    @Autowired
    private ICustInfoRetailService custInfoRetailService;
    @Autowired
    private ICustInfoBusinessService custInfoBusinessService;
    @Autowired
    private ICustInfoMerchantService custInfoMerchantService;
    @Autowired
    private IFamilyMembersService familyMembersService;
    @Autowired
    private ISignedProductsService signedProductsService;
    @Autowired
    private ISysIndustryService iSysIndustryService;


    /**
     * 查询对私客户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:retail:list')")
    @GetMapping("/list")
    @Log(title = "客户视图-查询对私客户信息列表")
    public TableDataInfo list(CustInfoRetail custInfoRetail) {
        startPage();
        List<CustInfoRetail> list = custInfoRetailService.selectCustInfoRetailList(custInfoRetail);
        return getDataTable(list);
    }

    /**
     * 根据关键词查询对私客户信息列表
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('system:retail:list')")
    @GetMapping("/listByKeyword")
    @ApiOperation("根据关键词查询客户列表")
    @Log(title = "客户视图-根据关键词查询客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词，可以是客户名，证件号，手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult listByKeyword(String keyword,Long pageNum,Long pageSize) {
        //startPage();
        Long start = (pageNum-1)*pageSize;
        List<CustInfoRetail> list = custInfoRetailService.selectCustInfoRetailListByKeyword(keyword,start,pageSize);
        return AjaxResult.success(list);
    }


    /**
     * 导出对私客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:retail:export')")
    @Log(title = "客户视图-对私客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustInfoRetail custInfoRetail) {
        List<CustInfoRetail> list = custInfoRetailService.selectCustInfoRetailList(custInfoRetail);
        ExcelUtil<CustInfoRetail> util = new ExcelUtil<CustInfoRetail>(CustInfoRetail.class);
        util.exportExcel(response, list, "对私客户信息数据");
    }

    /**
     * 获取对私客户信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:retail:query')")
    @GetMapping(value = "/getInfoByCustId")
    @Log(title = "客户视图-根据客户号查询客户360视图详情")
    @ApiOperation("根据客户号查询客户360视图详情")
    public AjaxResult getInfoByCustId(String custId) {
        return success(custInfoRetailService.selectCustInfoRetailVoByCustId(custId));
    }

    @GetMapping(value = "/getInfoByCustIdc")
    @ApiOperation("根据身份证号查询客户详情")
    @Log(title = "客户视图-根据身份证号查询客户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custIdc", value = "身份证号", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult getInfo(String custIdc) {
        return success(custInfoRetailService.selectCustInfoRetailBycustIdc(custIdc));
    }

    @GetMapping(value = "/getRetailInfoByCustId")
    @ApiOperation("根据客户号查询个人客户详情")
    @Log(title = "客户视图-根据客户号查询个人客户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "客户号", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult getCustRetailInfo(String custId) {
        return success(custInfoRetailService.selectCustInfoRetailByCustId(custId));
    }




    @GetMapping(value = "fold/{custIdc}")
    @Log(title = "客户视图-查询360视图客户详情折叠项")
    @ApiOperation("查询360视图客户详情折叠项")
    public AjaxResult getInfoVo(@PathVariable("custIdc") String custIdc) {

        return success(custInfoRetailService.selectCustInfoRetailFoldVoBycustIdc(custIdc));
    }


    /**
     * 新增对私客户信息
     */
//    @PreAuthorize("@ss.hasPermi('system:retail:add')")
    @Log(title = "客户视图-对私客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustInfoRetail custInfoRetail) {
        return toAjax(custInfoRetailService.insertCustInfoRetail(custInfoRetail));
    }


    @Log(title = "客户视图-对私客户信息营销建档", businessType = BusinessType.INSERT)
    @ApiOperation("对私客户营销建档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "relateMerchant", value = "关联商户名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "relateBusiness", value = "关联企业名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fmyName", value = "家庭成员名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fmyBusi", value = "家庭成员行业", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fmyTel", value = "家庭成员手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fmyRln", value = "成员与本人关系", dataType = "String", dataTypeClass = String.class),

    })
    @PostMapping("/addRecord")
    public AjaxResult add(CustInfoRetail custInfoRetail,String relateMerchant,String relateBusiness,String fmyName,String fmyBusi,String fmyTel,String fmyRln) {
        return custInfoRetailService.insertCustInfoRetailVo(custInfoRetail,relateMerchant,relateBusiness,fmyName,fmyBusi,fmyTel,fmyRln);
    }

    /**
     * 修改对私客户信息
     */
//    @PreAuthorize("@ss.hasPermi('system:retail:edit')")
    @Log(title = "客户视图-对私客户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("编辑客户详情")
    public AjaxResult edit(CustInfoRetail custInfoRetail) {
            return custInfoRetailService.updateCustInfoRetail(custInfoRetail);
    }

    /**
     * 删除对私客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:retail:remove')")
    @Log(title = "客户视图-对私客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(custInfoRetailService.deleteCustInfoRetailByIds(ids));
    }


    /**
     * 查询产业树
     */
    @GetMapping(value = "selectIndustryTree")
    @ApiOperation("查询产业树")
    public AjaxResult selectIndustryTree() {
        return AjaxResult.success(iSysIndustryService.getIndustryTree());
    }

    @GetMapping(value = "/exportRetailDetail")
    @ApiOperation("导出个人客户详情")
    public void exportInfo(HttpServletResponse response,String custId) throws Exception {
        custInfoRetailService.exportRetailDetail(response,custId);
    }

    @PostMapping(value = "/editCustTag")
    @ApiOperation("修改客户标签")
    public R<Objects> editCustTag(@RequestBody CustTagDTO custTagDTO)  {
        custInfoRetailService.editCustTag(custTagDTO);
        return R.ok();
    }

    @PostMapping(value = "/editManualTag")
    @ApiOperation("修改手动标签")
    public R<Objects> editManualTag(@RequestBody CustManualTagDTO custManualTagDTO)  {
        custInfoRetailService.editManualTag(custManualTagDTO);
        return R.ok();
    }

    @GetMapping("/manualTagList")
    @ApiOperation("获取自定义客户标签列表")
    public AjaxResult manualList(@Valid @NotNull String custType)
    {
        List<TreeNode> hashMaps = custInfoRetailService.selectmanualList(custType);
        return AjaxResult.success(hashMaps);
    }


}
