package com.fs.hisStore.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.company.mapper.CompanyUserRoleMapper;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.handwrite.domain.HandwriteCollection;
import com.fs.his.domain.FsPrescribe;
import com.fs.his.domain.FsUserInformationCollectionSchedule;
import com.fs.his.service.IFsPrescribeService;
import com.fs.hisStore.dto.FsUserInformationCollectionOverviewDTO;
import com.fs.hisStore.dto.PendingSalesInfoQueryDTO;
import com.fs.hisStore.param.FsUserInformationCollectionParam;
import com.fs.hisStore.param.bindCollectionPackageParam;
import com.fs.hisStore.vo.FsUserInformationCollectionOverviewVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.hisStore.domain.FsUserInformationCollection;
import com.fs.hisStore.service.IFsUserInformationCollectionService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 用户信息采集Controller
 * 
 * @author fs
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/hisStore/collection")
public class FsUserInformationCollectionController extends BaseController
{
    @Autowired
    private IFsUserInformationCollectionService fsUserInformationCollectionService;

    @Autowired
    private IFsPrescribeService prescribeService;

    @Autowired
    private CompanyUserRoleMapper roleMapper;

    /**
     * 查询用户信息采集列表
     */
    @PreAuthorize("@ss.hasPermi('hisStore:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserInformationCollection fsUserInformationCollection)
    {
        startPage();
        List<FsUserInformationCollection> list = fsUserInformationCollectionService.selectFsUserInformationCollectionList(fsUserInformationCollection);
        //电话号码脱敏
        for (FsUserInformationCollection item : list) {
            if (item.getUserPhoneFour() != null) {
                item.setUserPhoneFour(item.getUserPhoneFour().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户信息采集列表
     */
    @PreAuthorize("@ss.hasPermi('hisStore: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('hisStore:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserInformationCollectionService.selectFsUserInformationCollectionById(id));
    }

    /**
     * 新增用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('hisStore:collection:add')")
    @Log(title = "用户信息采集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserInformationCollectionParam fsUserInformationCollection)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsUserInformationCollection.setCompanyUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(fsUserInformationCollectionService.insertFsUserInformationCollection(fsUserInformationCollection));
    }

    /**
     * 修改用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('hisStore: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));
    }

    /**
     * 新增简化版用户信息采集(临时无userId)
     */
    @Log(title = "新增简化版用户信息采集(临时无userId)", businessType = BusinessType.INSERT)
    @PostMapping("/createSimpleUserInfo")
    public AjaxResult createSimpleUserInfo(@RequestBody FsUserInformationCollectionParam fsUserInformationCollection)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsUserInformationCollection.setCompanyUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(fsUserInformationCollectionService.createSimpleUserInfo(fsUserInformationCollection));
    }

    /**
     * 查询待销售完善信息采集列表
     * */
    @PostMapping("/pendingSalesInfoList")
    public TableDataInfo pendingSalesInfoList(@RequestBody PendingSalesInfoQueryDTO queryDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryDto.setCompanyUserId(loginUser.getUser().getUserId());
        //管理员查看所有数据
        Long isAdmin = roleMapper.companyUserIsAdmin(queryDto.getCompanyUserId());
        if (isAdmin != null) {
            queryDto.setCompanyUserId(null);
        }
        List<FsUserInformationCollectionOverviewVo> list = fsUserInformationCollectionService.pendingSalesInfoList(queryDto);
        //电话号码脱敏
        for (FsUserInformationCollectionOverviewVo item : list) {
            if (item.getUserPhoneFour() != null) {
                item.setUserPhoneFour(item.getUserPhoneFour().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }
    /**
     * 提交待销售完善信息采集列表
     * */
    @PostMapping("/submitSalesInfo")
    public AjaxResult submitSalesInfo(@RequestBody FsUserInformationCollectionParam param){
        fsUserInformationCollectionService.submitSalesInfo(param);
        return AjaxResult.success();
    }
    /**
     * 删除用户信息采集
     */
    @PreAuthorize("@ss.hasPermi('hisStore: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('hisStore:collection:WxaCodeCollectionUnLimit')")
    @GetMapping("/getWxaCodeCollectionUnLimit/{collectionId}/{appId}")
    public R getWxaCodeCollectionUnLimit(@PathVariable("collectionId") Long collectionId,@PathVariable("appId") String appId)
    {

        return fsUserInformationCollectionService.getWxaCodePackageOrderUnLimit(collectionId,appId);

    }

    //@PreAuthorize("@ss.hasPermi('hisStore:collection:WxaCodeCollectionUnLimitNew')")
    @GetMapping("/getWxaCodeCollectionUnLimitNew/{collectionId}/{appId}")
    public R getWxaCodeCollectionUnLimitNew(@PathVariable("collectionId") Long collectionId,@PathVariable("appId") String appId)
    {

        return fsUserInformationCollectionService.getWxaCodePackageOrderUnLimitNew(collectionId,appId);

    }

    /**
     * 信息采集取消
     */
    @PreAuthorize("@ss.hasPermi('qw:collectionSchedule:stop')")
    @PutMapping("/stop")
    public R stop(@RequestBody FsUserInformationCollectionSchedule param){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return fsUserInformationCollectionService.stopCollection(param,loginUser.getUser().getUserId(),1);
    }

    /**
     * 销售确认
     * */
    @PutMapping("/salesHelpConfirm")
    public R salesHelpConfirm(@RequestBody FsUserInformationCollectionSchedule param){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return fsUserInformationCollectionService.salesHelpConfirm(param.getCollectionId(),loginUser.getUser().getUserId());
    }

    /**
     * 查询未绑定的信息采集列表
     */
    @PostMapping("/getUnBindCollectionList")
    public TableDataInfo getUnBindCollectionList(@RequestBody FsUserInformationCollectionOverviewDTO queryDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryDto.setCompanyUserId(loginUser.getUser().getUserId());
        //管理员查看所有数据
        Long isAdmin = roleMapper.companyUserIsAdmin(queryDto.getCompanyUserId());
        if (isAdmin != null) {
            queryDto.setCompanyUserId(null);
        }
        startPage();
        List<FsUserInformationCollectionOverviewVo> list =fsUserInformationCollectionService.getUnBindCollectionList(queryDto);
        //电话号码脱敏
        for (FsUserInformationCollectionOverviewVo item : list) {
            if (item.getUserPhoneFour() != null) {
                item.setUserPhoneFour(item.getUserPhoneFour().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询销售已完善信息采集列表
     */
    @PostMapping("/fullyCollectionInfoList")
    public TableDataInfo fullyCollectionInfoList(@RequestBody FsUserInformationCollectionOverviewDTO queryDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryDto.setCompanyUserId(loginUser.getUser().getUserId());
        //管理员查看所有数据
        Long isAdmin = roleMapper.companyUserIsAdmin(queryDto.getCompanyUserId());
        if (isAdmin != null) {
            queryDto.setCompanyUserId(null);
        }
        startPage();
        List<FsUserInformationCollectionOverviewVo> list =fsUserInformationCollectionService.fullyCollectionInfoList(queryDto);
        //电话号码脱敏
        for (FsUserInformationCollectionOverviewVo item : list) {
            if (item.getUserPhoneFour() != null) {
                item.setUserPhoneFour(item.getUserPhoneFour().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 销售绑定个微信息采集套餐包
     * */
    @PostMapping("/bindCollectionPackage")
    public AjaxResult bindCollectionPackage(@RequestBody bindCollectionPackageParam param){
        Long collectionId = fsUserInformationCollectionService.bindCollectionPackage(param);
        if (collectionId>0){
            return AjaxResult.success(collectionId);
        }
        return AjaxResult.error();
    }

    /**
     * 查询个微采集套餐包信息
     * */
    @GetMapping("/getCollectionPackageInfo")
    public AjaxResult getCollectionPackageInfo(@RequestParam Long collectionId){
        return AjaxResult.success(fsUserInformationCollectionService.getCollectionPackageInfo(collectionId));
    }
    
    /**
     * 根据用户id最新审核通过处方信息
     * */
    @GetMapping("/getLastAuditPassPrescribeInfo")
    public AjaxResult getLastAuditPassPrescribeInfo(@RequestParam Long collectionId){
        FsPrescribe fsPrescribe = prescribeService.selectNewestAuditedPrescribeByCollectionId(collectionId);
        return AjaxResult.success(fsPrescribe);
    }

    /**
     * 查询销售是否具有代填权限
     * */
    @GetMapping("/getSalesHasCollectionPermission")
    public AjaxResult getSalesHasCollectionPermission(){
        boolean salesProxyFillConfig = fsUserInformationCollectionService.getSalesProxyFillConfig();
        return AjaxResult.success(salesProxyFillConfig);
    }

    /**
     * 重置CID信息采集
     * @param param 参数
     * @return 是否重置成功
     */
    @PostMapping("/resetCIDCollection")
    public AjaxResult resetCIDCollection(@RequestBody FsUserInformationCollection param){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long operatorId=loginUser.getUser().getUserId();
        String nickName=loginUser.getUser().getNickName();
        return AjaxResult.success(fsUserInformationCollectionService.resetCIDCollection(param.getId(),operatorId,nickName));
    }
}
