package com.ruoyi.web.controller.api;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.Vo.MallIndustryVo;
import com.ruoyi.mall.domain.Vo.SchoolMajorVO;
import com.ruoyi.web.controller.spi.CheckSensitiveWordSpi;
import com.ruoyi.mall.domain.MallIndustry;
import com.ruoyi.mall.domain.MallIndustryAnalysis;
import com.ruoyi.mall.domain.MallLinkIndustryCareer;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.service.*;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName 发展方向接口
 * @Description 发展方向接口
 * @Author LCL
 * @Date 2023/10/18
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/developDirection")
public class ApiDevelopDirectionController {

    @Resource
    private IMallMemberUserService mallMemberUserService;

    @Resource
    private IMallMajorClassService mallMajorClassService;

    @Resource
    private IMallCareerService mallCareerService;

    @Resource
    private IMallCareerSalaryService mallCareerSalaryService;

    @Resource
    private IMallIndustryService mallIndustryService;

    @Resource
    private IMallIndustryAnalysisService mallIndustryAnalysisService;

    @Resource
    private IMallLinkIndustryCareerService mallLinkIndustryCareerService;

    @Resource
    private IMallConfigService mallConfigService;


    /**
     * 用户的已设方向
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/getMemberDevelopDirection")
    public AjaxResult getMemberDevelopDirection(@RequestParam Long userId) {
        try {
            return AjaxResult.success().put("data", mallMemberUserService.selectMallMemberUserById(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("用户信息异常");
        }
    }

    /**
     * 设置用户的发展方向
     *
     * @param params userId           用户ID
     *               developDirection 发展方向：0-未设置，1-以就业为导向，2-以学科专业为导向，3-已进体制内为导向，4-以保研/考研为导向，
     * @return
     */
    @PostMapping("/setMemberDevelopDirection")
    public AjaxResult setMemberDevelopDirection(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userId") || StringUtils.isEmpty(params.get("userId").toString())) {
            return AjaxResult.error("缺少用户IDuserId");
        }
        if (!params.containsKey("developDirection") || StringUtils.isEmpty(params.get("developDirection").toString())) {
            return AjaxResult.error("缺少发展方向developDirection");
        }
        try {
            long userId = Long.parseLong(params.get("userId").toString());
            String developDirection = params.get("developDirection").toString();

            MallMemberUser memberUser = new MallMemberUser();
            memberUser.setId(userId);
            memberUser.setDevelopDirection(developDirection);
            return AjaxResult.success().put("data", mallMemberUserService.updateMallMemberUser(memberUser));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("设置用户的发展方向异常");
        }
    }

    /**
     * 发展方向-职业列表
     *
     * @param pageNum  页码
     * @param pageSize 每页个数
     * @param sortType 排序类型：0-默认，1-从高到低，2-从低到高
     * @param majorId  专业分类ID
     * @return
     */
    @GetMapping("/getCareerList")
    public AjaxResult getMajorProspect(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(required = false) Integer sortType,
                                       @RequestParam(required = false) Long majorId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> mapList = mallCareerService.selectCareerListByParams(sortType, majorId);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(mapList);
            return AjaxResult.success().put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发展方向-职业列表异常");
        }
    }

    /**
     * 发展方向-行业列表
     *
     * @param pageNum  页码
     * @param pageSize 每页个数
     * @return
     */
    @GetMapping("/getIndustryList")
    public AjaxResult getIndustryList(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        try {

            List<MallIndustryVo> list = mallIndustryService.selectMallIndustryListForxcx(new MallIndustry());
            if(null != list && list.size()>0){
                Page page = new Page<>(pageNum,pageSize);
                //为Page类中的total属性赋值
                int total = list.size();
                page.setTotal(total);
                //计算当前需要显示的数据下标起始值
                int startIndex = (pageNum - 1) * pageSize;
                int endIndex = Math.min(startIndex + pageSize,total);
                //从链表中截取需要显示的子链表，并加入到Page
                if(list.size()>pageSize) {
                    page.addAll(list.subList(startIndex, endIndex));
                }else{
                    page.addAll(list);
                }
                //以Page创建PageInfo
                PageInfo pageInfo = new PageInfo<>(page);
                return AjaxResult.success().put("data",pageInfo);
            }else{
                PageInfo<MallIndustryVo> pageInfo = new PageInfo<>(new ArrayList<>());
                return AjaxResult.success().put("data", pageInfo);
            }


        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发展方向-行业列表异常");
        }
    }

    /**
     * 发展方向-行业分析详情
     *
     * @param industryId 行业ID
     * @return
     */
    @GetMapping("/getIndustryAnalysisDetails")
    public AjaxResult getIndustryAnalysisDetails(@RequestParam Long industryId) {
        try {
            MallIndustryAnalysis mallIndustryAnalysis = new MallIndustryAnalysis();
            mallIndustryAnalysis.setIndustryId(industryId);
            List<MallIndustryAnalysis> list = mallIndustryAnalysisService.selectMallIndustryAnalysisList(mallIndustryAnalysis);
            return AjaxResult.success().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发展方向-行业分析详情异常");
        }
    }

    /**
     * 发展方向-相关职业列表
     *
     * @param industryId 行业ID
     * @param pageNum    页码
     * @param pageSize   每页个数
     * @return
     */
    @GetMapping("/getLinkIndustryCareerList")
    public AjaxResult getLinkIndustryCareerList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                @RequestParam Long industryId) {
        try {
            MallLinkIndustryCareer linkIndustryCareer = new MallLinkIndustryCareer();
            linkIndustryCareer.setIndustryId(industryId);
            PageHelper.startPage(pageNum, pageSize);
            List<MallLinkIndustryCareer> list = mallLinkIndustryCareerService.selectMallLinkIndustryCareerList(linkIndustryCareer);
            PageInfo<MallLinkIndustryCareer> pageInfo = new PageInfo<>(list);
            return AjaxResult.success().put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发展方向-行业列表异常");
        }
    }

    /**
     * 发展方向-进体制为导向
     *
     * @param mark opportunity：美术生进体制机会，difficulty：进体制难度
     * @return
     */
    @GetMapping("/getInstitutionalDetails")
    public AjaxResult getInstitutionalDetails(@RequestParam String mark) {
        try {
            return AjaxResult.success().put("data", mallConfigService.selectMallConfigByMarK(mark));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发展方向-进体制为导向异常");
        }
    }

}
