package com.xhkjedu.sapi.controller.assess;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.assess.TAssessUser;
import com.xhkjedu.sapi.service.assess.AssessUserService;
import com.xhkjedu.sapi.utils.PoiUtils;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author ywx
* @classname TAssessUserController
* @description
* @date 2021/11/10 10:31
**/
@RestController
@RequestMapping("/auser")
public class AssessUserController {
    @Resource
    private AssessUserService assessUserService;

    @PostMapping("/add")
    @Action("评价录入-增加用户")
    public ResultVo add(@RequestBody TAssessUser user) {
        TAssessUser assessUser = assessUserService.save(user);
        return new ResultVo(0,"保存成功",assessUser);
    }

    @PostMapping("/delete")
    @Action("评价录入-删除用户")
    public ResultVo delete(@RequestBody TAssessUser user) {
        Integer auid = user.getAuid();
        N_Utils.validation(new Object[]{auid,"评价用户id",1});
        assessUserService.deleteById(auid);
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TAssessUser user) {
        N_Utils.validation(new Object[]{user.getUserid(),"用户id",1,user.getQuotaid(),"指标id",1
                ,user.getAutype(),"评价类型",1,user.getAssessid(),"评价id",1});
        List<Map> list = assessUserService.listByAssess(user);
        return new ResultVo(0, "获取详情成功", list);
    }

    @PostMapping("/adetail")
    public ResultVo adetail(@RequestBody TAssessUser user) {
        N_Utils.validation(new Object[]{user.getUserid(),"用户id",1,user.getQuotaid(),"指标id",1,user.getAutype(),"评价类型",1
                ,user.getTemplateid(),"模板id",1,user.getPage(),"页码",1,user.getPageSize(),"显示条数",1});
        PageHelper.startPage(user.getPage(), user.getPageSize());
        List<Map> list = assessUserService.listByAssess(user);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取详情成功", pageResult);
    }

    @PostMapping("/list_file")
    public ResultVo listFile(@RequestBody TAssessUser user) {
        Integer auid = user.getAuid();
        N_Utils.validation(new Object[]{auid, "评价用户id", 1});
        List<Map> list = assessUserService.listFile(auid);
        return new ResultVo(0, "获取列表成功", list);
    }

    @PostMapping("/import")
    @Action("评价录入-导入用户")
    public ResultVo importAssess(TAssessUser user,MultipartFile file) {
        Integer schoolid = user.getSchoolid();
        Integer assessid = user.getAssessid();
        Integer templateid = user.getTemplateid();
        Integer createid = user.getCreateid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, assessid, "评价id", 1
                , templateid, "评价模板id", 1, createid, "创建人id", 1});
        Map<String, Object> map = PoiUtils.readAssess(file, schoolid, templateid, assessUserService);
        Integer code = (Integer) map.get("code");
        if (code != 0) return new ResultVo(1, map.get("msg").toString());
        LinkedHashMap<Integer, List<String>> errMsg = (LinkedHashMap<Integer, List<String>>) map.get("errMsg");
        if (errMsg.size() != 0) return new ResultVo(1, null, errMsg);
        List<TAssessUser> users = (List<TAssessUser>) map.get("users");
        users.forEach(u -> {
            u.setAssessid(assessid);
            u.setTemplateid(templateid);
            u.setCreateid(createid);
            u.setCreatetime(N_Utils.getSecondTimestamp());
        });
        assessUserService.batchSaveAssessUser(users);
        return new ResultVo(0, "导入评价用户成功");
    }
}
