package com.flossom.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.shaded.com.google.common.base.Function;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.entity.WxNursingLog;
import com.flossom.common.core.domain.entity.WxUserInstrument;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.domain.entity.WxUserTag;
import com.flossom.common.core.domain.export.WxNursingLogExport;
import com.flossom.common.core.domain.export.WxUserMemberExport;
import com.flossom.common.core.domain.req.UserNursingLogReq;
import com.flossom.common.core.domain.req.WxNursingLogExportVm;
import com.flossom.common.core.domain.req.WxUserMemberExportVm;
import com.flossom.common.core.domain.req.WxUserMemberVm;
import com.flossom.common.core.domain.ret.WxUserMemberRet;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.system.service.IWxNursingLogService;
import com.flossom.system.service.IWxUserMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户护理日志Controller
 *
 * @author flossom
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/nursingLog")
public class WxNursingLogController extends BaseController {

    @Autowired
    private IWxNursingLogService wxNursingLogService;

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    /**
     * 查询用户护理日志列表
     */
    @RequiresPermissions("system:nursingLog:list")
    @GetMapping("/list")
    public TableDataInfo list(UserNursingLogReq userNursingLogReq) {
        List<WxNursingLog> list = wxNursingLogService.selectWxNursingLogList(userNursingLogReq, true);
        return getDataTable(list);
    }

    /**
     * 获取用户护理日志详细信息
     */
    @RequiresPermissions("system:nursingLog:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxNursingLogService.selectWxNursingLogById(id));
    }

    /**
     * 删除用户护理日志
     */
    @RequiresPermissions("system:nursingLog:remove")
    @Log(title = "用户护理日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxNursingLogService.deleteWxNursingLogByIds(ids));
    }

    /**
     * 全量添加小程序标签
     */
    @PostMapping("/allAddMiniProgramTag")
    public R allAddMiniProgramTag(@RequestParam("tagIdList") List<Integer> tagIdList, @RequestBody UserNursingLogReq userNursingLogReq) {
        if (tagIdList == null || tagIdList.size() == 0) {
            logger.error("参数有误");
            throw new ServiceException("参数有误");
        }

        List<WxNursingLog> list = wxNursingLogService.selectWxNursingLogList(userNursingLogReq, false);
        if (list != null && list.size() > 0) {
            List<Integer> collect = list.stream().map(wxNursingLog -> wxNursingLog.getUserId().intValue()).collect(Collectors.toList());
            wxUserMemberService.batchAddMiniProgramTag(tagIdList, collect);
        }
        return R.ok();
    }

    /**
     * 全量 删除小程序标签
     */
    @PostMapping("/allDelMiniProgramTag")
    public R allDelMiniProgramTag(@RequestParam("tagIdList") List<Integer> tagIdList, @RequestBody UserNursingLogReq userNursingLogReq) {
        if (tagIdList == null || tagIdList.size() == 0) {
            logger.error("参数有误");
            throw new ServiceException("参数有误");
        }
        List<WxNursingLog> list = wxNursingLogService.selectWxNursingLogList(userNursingLogReq, false);
        if (list != null && list.size() > 0) {
            List<Integer> collect = list.stream().map(wxNursingLog -> wxNursingLog.getUserId().intValue()).collect(Collectors.toList());
            wxUserMemberService.batchDelMiniProgramTag(tagIdList, collect);
        }
        return R.ok();
    }


    /**
     * 批量导出护理记录数据
     */
    @RequiresPermissions("system:nursingLog:export")
    @PostMapping("/batchExport")
    public void batchExport(HttpServletResponse response, WxNursingLogExportVm wxNursingLogExportVm) {
        List<Integer> idList = wxNursingLogExportVm.getIdList();
        if (idList == null || idList.size() == 0) {
            throw new ServiceException("请选择导出数据");
        }
        List<WxNursingLog> list = wxNursingLogService.selectWxNursingLogByIdList(wxNursingLogExportVm.getIdList());
        export(response, list, wxNursingLogExportVm.getExportFields());
    }

    /**
     * 全量导出护理记录数据
     */
    @RequiresPermissions("system:nursingLog:export")
    @PostMapping("/allExport")
    public void allExport(HttpServletResponse response, WxNursingLogExportVm wxNursingLogExportVm) {
        List<WxNursingLog> list = wxNursingLogService.selectWxNursingLogList(wxNursingLogExportVm, true);
        export(response, list, wxNursingLogExportVm.getExportFields());
    }

    private static void export(HttpServletResponse response, List<WxNursingLog> list, List<String> exportFields) {
        List<WxNursingLogExport> exportList = new ArrayList<>();
        WxNursingLogExport export;
        for (WxNursingLog wxNursingLog : list) {
            export = new WxNursingLogExport();
            BeanUtils.copyProperties(wxNursingLog, export);
            exportList.add(export);
        }
        // 添加默认导出字段
        exportFields.add("instrumentName");
        exportFields.add("modeName");
        exportFields.add("online");
        exportFields.add("accompanyMode");
        exportFields.add("createTime");
        exportFields.add("nursingTime");
        exportFields.add("gearPositionOne");
        exportFields.add("gearPositionTwo");
        exportFields.add("gearPositionThree");
        exportFields.add("gearPositionFour");
        exportFields.add("energyValue");
        exportFields.add("waterDataOne");
        exportFields.add("waterDataTwo");
        exportFields.add("waterDataThree");

        ExcelUtil<WxNursingLogExport> util = new ExcelUtil(WxNursingLogExport.class);
        util.exportExcel(response, exportList, "护理记录数据", exportFields);
    }

}
