package com.fs.company.controller.store;

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.poi.ExcelUtil;
import com.fs.company.domain.CompanyUserUser;
import com.fs.company.service.ICompanyUserUserService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsUserInformationCollection;
import com.fs.his.param.FsUserInformationCollectionParam;
import com.fs.his.service.IFsUserInformationCollectionService;
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 2025-10-14
 */
@RestController
@RequestMapping("/store/collection")
public class FsUserInformationCollectionController extends BaseController
{
    @Autowired
    private IFsUserInformationCollectionService fsUserInformationCollectionService;
    @Autowired
    private ICompanyUserUserService companyUserUserService;

    /**
     * 查询用户信息采集列表
     */
    @PreAuthorize("@ss.hasPermi('his:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserInformationCollection fsUserInformationCollection)
    {
        startPage();
        List<FsUserInformationCollection> list = fsUserInformationCollectionService.selectFsUserInformationCollectionList(fsUserInformationCollection);
        return getDataTable(list);
    }

    /**
     * 导出用户信息采集列表
     */
    @PreAuthorize("@ss.hasPermi('his:collection:export')")
    @Log(title = "用户信息采集", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserInformationCollection fsUserInformationCollection)
    {
        List<FsUserInformationCollection> list = fsUserInformationCollectionService.selectFsUserInformationCollectionList(fsUserInformationCollection);
        ExcelUtil<FsUserInformationCollection> util = new ExcelUtil<FsUserInformationCollection>(FsUserInformationCollection.class);
        return util.exportExcel(list, "用户信息采集数据");
    }

    /**
     * 获取用户信息采集详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserInformationCollectionService.selectFsUserInformationCollectionById(id));
    }

    /**
     * 新增用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('his:collection:add')")
    @Log(title = "用户信息采集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserInformationCollectionParam fsUserInformationCollection)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long companyUserId = loginUser.getUser().getUserId();
        //查询用户是否绑定销售
        Long userId = fsUserInformationCollection.getUserId();
        if (userId == null || userId < 0){
            return AjaxResult.error("请选择绑定用户");
        }
        CompanyUserUser companyUserUser = new CompanyUserUser();
        companyUserUser.setCompanyUserId(companyUserId);
        companyUserUser.setUserId(userId);
        List<CompanyUserUser> companyUserUsers = companyUserUserService.selectCompanyUserUserList(companyUserUser);
        if (companyUserUsers == null || companyUserUsers.isEmpty()){
            return AjaxResult.error("用户未绑定该销售");
        }
        fsUserInformationCollection.setCompanyUserId(companyUserId);
        return AjaxResult.success(fsUserInformationCollectionService.insertFsUserInformationCollection(fsUserInformationCollection));
    }

    /**
     * 修改用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('his:collection:edit')")
    @Log(title = "用户信息采集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserInformationCollectionParam fsUserInformationCollection)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsUserInformationCollection.setCompanyUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(fsUserInformationCollectionService.updateFsUserInformationCollection(fsUserInformationCollection));
    }

    /**
     * 删除用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('his:collection:remove')")
    @Log(title = "用户信息采集", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserInformationCollectionService.deleteFsUserInformationCollectionByIds(ids));
    }

    @GetMapping("/getInfo")
    public AjaxResult getInfo(FsUserInformationCollection fsUserInformationCollection){
        return AjaxResult.success(fsUserInformationCollectionService.getInfo(fsUserInformationCollection));
    }

    /**
     * 生成套餐包付款二维码
     */
    @PreAuthorize("@ss.hasPermi('his:collection:WxaCodeCollectionUnLimit')")
    @GetMapping("/getWxaCodeCollectionUnLimit/{collectionId}")
    public R getWxaCodeCollectionUnLimit(@PathVariable("collectionId") Long orderId)
    {

        return fsUserInformationCollectionService.getWxaCodePackageOrderUnLimit(orderId);

    }
}
