package com.rongwei.training.controller;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rongwei.rwcommon.base.QueryPar;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.training.domain.EmpCourseDetailDo;
import com.rongwei.training.domain.EmpCourseDo;
import com.rongwei.trainingcommon.sys.service.EmpCourseDetailService;
import com.rongwei.trainingcommon.sys.service.EmpCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 员工课程详情记录表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-09-07 17:59:22
 */
@RestController
@RequestMapping("emp/course/detail")
@Slf4j
public class EmpCourseDetailController {
    @Autowired
    private EmpCourseDetailService empCourseDetailService;
    @Autowired
    private EmpCourseService empCourseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody QueryPar<EmpCourseDetailDo> params) {
        try {
            Wrapper<EmpCourseDetailDo> entity = new QueryWrapper(params.getParameter());
            List<EmpCourseDetailDo> list = empCourseDetailService.list(entity);
            return R.ok(list);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.queryError();
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        try {
            EmpCourseDetailDo empCourseDetail = empCourseDetailService.getById(id);
            return R.ok(empCourseDetail);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.queryError();
        }
    }

    /**
     * 保存or更新
     */
    @PostMapping("/saveOrUpdate")
    public R add(@RequestBody EmpCourseDetailDo empCourseDetail) {
        try {
            empCourseDetailService.saveOrUpdate(empCourseDetail);
            return R.saveOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }

    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody List<String> ids) {
        try {
            empCourseDetailService.removeByIds(ids);
            return R.deleteOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.deleteError();
        }
    }

    @PostMapping("/updateEmpTrainingTime")
    public R updateEmpTrainingTime(@RequestBody EmpCourseDetailDo empCourseDetail) {
        try {
            DateBetween dateBetween = new DateBetween(empCourseDetail.getStarttime(), empCourseDetail.getEndtime(), true);
            long useTime = dateBetween.between(DateUnit.SECOND);
            empCourseDetail.setCourseprogress(useTime);
            empCourseDetail.setId(SecurityUtil.getUUID());
            empCourseDetailService.save(empCourseDetail);
            Map<String, Object> query = new HashMap<>();
            query.put("empcourseid", empCourseDetail.getEmpcourseid());
            List<EmpCourseDetailDo> list = (List<EmpCourseDetailDo>) empCourseDetailService.listByMap(query);
            int total = 0;
            if (list != null && list.size() > 0) {
                total = list.stream().map(x -> Integer.parseInt(String.valueOf(x.getCourseprogress()))).reduce(Integer::sum).get();
            }
            // 总用时
            empCourseDetail.setCoursefiletime(total);
            empCourseDetail.getEmpcourseid();
            EmpCourseDo empCourse = empCourseService.getById(empCourseDetail.getEmpcourseid());
            empCourse.setCourseusetime(total);
            empCourseService.updateById(empCourse);
            return R.ok().putData(empCourseDetail);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error(StringUtils.spliceErrorMsg(e));
        }
    }

}
