package jnpf.cultivate.controller.web.teaching;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.TeachingRecordPracticeService;
import jnpf.model.cultivate.dto.teaching.*;
import jnpf.model.cultivate.vo.teaching.*;
import jnpf.util.CustomTenantUtil;
import jnpf.util.FtbUtil;
import jnpf.util.NoDataSourceBind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 练习管理
 */
@Slf4j
@RestController
@RequestMapping("/teachingRecord")
public class TeachingRecordController {
    @Resource
    private CustomTenantUtil customTenantUtil;
    @Autowired
    private TeachingRecordPracticeService recordPracticeService;

    /**
     * Web-练习门店下拉
     *
     * @return 练习门店集合
     */
    @PostMapping("/web/getStoreList")
    public ActionResult<List<StoreInfoListVo>> getStoreList() {
        return ActionResult.success(recordPracticeService.getStoreList());
    }

    /**
     * Web汇总-分页列表
     *
     * @param pageDto 查询参数
     * @return 汇总数据集合
     */
    @PostMapping("/web/summaryPageList")
    public ActionResult<PageListVO<SummaryPageListVo>> summaryPageList(@Valid @RequestBody TeachingBaseFilter pageDto) {
        PageInfo<SummaryPageListVo> page = recordPracticeService.summaryPageList(pageDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * Web汇总-导出
     * @param response 响应
     * @param pageDto 查询参数
     */
    @PostMapping("/web/summaryExport")
    public void summaryExport(HttpServletResponse response, @Valid @RequestBody TeachingBaseFilter pageDto) throws Exception {
        recordPracticeService.summaryExport(response, pageDto);
    }

    /**
     * Web记录-分页列表
     *
     * @param pageDto 查询参数
     * @return 练习集合
     */
    @PostMapping("/web/recordPageList")
    public ActionResult<PageListVO<RecordPageListVo>> recordPageList(@Valid @RequestBody TeachingBaseFilter pageDto) {
        PageInfo<RecordPageListVo> page = recordPracticeService.recordPageList(pageDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * Web记录-导出
     * @param response 响应
     * @param pageDto 查询参数
     */
    @PostMapping("/web/recordExport")
    public void recordExport(HttpServletResponse response, @Valid @RequestBody TeachingBaseFilter pageDto) throws Exception {
        recordPracticeService.recordExport(response, pageDto);
    }

    /**
     * Web记录-批量删除
     * @param deleteDto 删除参数
     * @return 是否成功
     */
    @DeleteMapping("/web/recordBatchDelete")
    public ActionResult<Boolean> recordBatchDelete(@Valid @RequestBody RecordBatchDeleteDto deleteDto) {
        return ActionResult.success(recordPracticeService.recordBatchDelete(deleteDto));
    }

    /**
     * App我的练习-分页列表
     *
     * @param pageDto 查询参数
     * @return 练习集合
     */
    @PostMapping("/app/myRecordPageList")
    public ActionResult<PageListVO<MyRecordPageListVo>> myRecordPageList(@Valid @RequestBody MyRecordPageListDto pageDto) {
        PageInfo<MyRecordPageListVo> page = recordPracticeService.myRecordPageList(pageDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * App我的练习-新增
     *
     * @param saveDto 保存参数
     * @return 是否成功
     */
    @PostMapping("/app/mySave")
    public ActionResult<Boolean> mySave(@Valid @RequestBody RecordSaveDto saveDto) throws Exception {
        return ActionResult.success(recordPracticeService.mySave(saveDto));
    }

    /**
     * App我的练习-详情
     *
     * @param id 数据id
     * @return 练习详情
     */
    @PostMapping("/app/myInfo/{id}")
    public ActionResult<RecordInfoVo> myInfo(@PathVariable("id") String id) {
        return ActionResult.success(recordPracticeService.myInfo(id));
    }

    /**
     * App我的练习-修改
     *
     * @param updateDto 修改参数
     * @return 是否成功
     */
    @PutMapping("/app/myUpdate")
    public ActionResult<Boolean> myUpdate(@Valid @RequestBody RecordUpdateDto updateDto) {
        return ActionResult.success(recordPracticeService.myUpdate(updateDto));
    }

    /**
     * App我的练习-删除
     *
     * @param id 数据id
     * @return 是否成功
     */
    @DeleteMapping("/app/myDelete/{id}")
    public ActionResult<Boolean> myDelete(@PathVariable("id") String id) {
        return ActionResult.success(recordPracticeService.myDelete(id));
    }

    /**
     * App我的练习-查看数据
     *
     * @param viewDataDto 查询条件
     * @return 技能点信息集合
     */
    @PostMapping("/app/myViewData")
    public ActionResult<ViewDataVo> myViewData(@Valid @RequestBody ViewDataDto viewDataDto) {
        return ActionResult.success(recordPracticeService.myViewData(viewDataDto));
    }

    /**
     * App员工练习-分页列表
     *
     * @param pageDto 查询参数
     * @return 练习集合
     */
    @PostMapping("/app/employeePageList")
    public ActionResult<PageListVO<EmployeePageListVo>> employeePageList(@Valid @RequestBody EmployeePageListDto pageDto) {
        PageInfo<EmployeePageListVo> page = recordPracticeService.employeePageList(pageDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * App员工练习-查看数据
     *
     * @param viewDataDto 查询参数
     * @return 练习集合
     */
    @PostMapping("/app/employeeViewData")
    public ActionResult<List<EmployeeViewDataVo>> employeeViewData(@Valid @RequestBody ViewDataDto viewDataDto) {
        return ActionResult.success(recordPracticeService.employeeViewData(viewDataDto));
    }

    /**
     * App店长界面-今日练习汇总数据
     *
     * @param storeId 查询参数
     * @return 练习汇总数据
     */
    @GetMapping("/app/getTodaySummary")
    public TodaySummaryDataVo getTodaySummary(@RequestParam("storeId") String storeId) {
        return recordPracticeService.getTodaySummary(storeId);
    }

    /**
     * 生成带教/练习的模拟数据
     */
    @NoDataSourceBind
    @GetMapping("/createSimulatedData")
    public ActionResult<Boolean> createSimulatedData(@RequestParam String tenantId) {
        customTenantUtil.checkOutTenant(tenantId);
        return ActionResult.success(recordPracticeService.createSimulatedData(tenantId));
    }

    /**
     * A员工界面--我的练习汇总数据
     *
     * @param storeId 查询参数
     * @return 练习汇总数据
     */
    @GetMapping("/app/getMyPracticeSummary")
    public MyPracticeSummaryVo getMyPracticeSummary(@RequestParam("storeId") String storeId) {
        return recordPracticeService.getMyPracticeSummary(storeId);
    }
}