package org.jeecg.modules.userCode.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.banner.entity.WebBanner;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.industry.entity.TbIndustry;
import org.jeecg.modules.industry.service.ITbIndustryService;
import org.jeecg.modules.postBean.SuggestReq;
import org.jeecg.modules.tbSuggest.entity.TbSuggest;
import org.jeecg.modules.tbSuggest.service.ITbSuggestService;
import org.jeecg.modules.tbTask.service.ITbTaskService;
import org.jeecg.modules.userCode.mapper.TaskMapper;
import org.jeecg.modules.userCode.model.vo.TbTaskVo;
import org.jeecg.modules.userCode.model.vo.UserRole1Vo;
import org.jeecg.modules.userCode.service.IndexService;
import org.jeecg.modules.userRole.entity.TbUserRole;
import org.jeecg.modules.userRole.service.ITbUserRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/index")
@Api(tags = "首页等展示接口")
@Slf4j
public class IndexController {
    @Resource
    private ITbSuggestService tbSuggestService;
    @Resource
    private IndexService indexService;
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private ITbIndustryService tbIndustryService;
    @Resource
    private ITbUserRoleService tbUserRoleService;
    @Resource
    private ITbTaskService tbTaskService;
    @Resource
    private TaskMapper TaskMapper;

    @ApiOperation(value = "我的头像昵称，平台客户电话等信息")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Result index(@RequestHeader("X-Access-Token") String token) {

        Result<?> indexInfo = indexService.getIndexInfo(token);

        return indexInfo;
    }


    @ApiOperation(value = "获取平台隐私政策，协议等")
    @RequestMapping(value = "/getSysText", method = RequestMethod.POST)
    public Result getSysText() {

        Result<?> indexInfo = indexService.getSysText();

        return indexInfo;
    }



    @ApiOperation(value = "获取banner列表")
    @RequestMapping(value = "/bannerList", method = RequestMethod.POST)
    public Result<Map<Integer, List<WebBanner>>> bannerList() {

        return indexService.bannerList();
    }


    @ApiOperation(value = "帮助与反馈", notes = "帮助与反馈")
    @PostMapping(value = "/addSuggest")
    public Result<String> addSuggest(@RequestBody SuggestReq tbSuggest, @RequestHeader("X-Access-Token") String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        TbSuggest tbSuggest1 = new TbSuggest();
        BeanUtils.copyProperties(tbSuggest, tbSuggest1);
        tbSuggest1.setUserId(hanHaiMember.getId());
        tbSuggestService.save(tbSuggest1);
        return Result.OK("添加成功！");
    }


    @ApiOperation(value = "首页行业菜单", notes = "首页行业菜单")
    @GetMapping(value = "/industryList")
    public Result<List<TbIndustry>> queryPageList() {
        QueryWrapper<TbIndustry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0).eq("del_flag", 0).orderByDesc("sort_no");
        List<TbIndustry> list = tbIndustryService.list(queryWrapper);
        return Result.OK(list);
    }


    @ApiOperation(value = "根据行业id查询工种", notes = "根据行业id查询工种")
    @GetMapping(value = "/getIndustryById")
    public Result<List<TbIndustry> > getIndustryById(@RequestParam(name = "pid", required = true) String pid) {
        QueryWrapper<TbIndustry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid).eq("del_flag", 0).orderByDesc("sort_no");
        List<TbIndustry> list = tbIndustryService.list(queryWrapper);
        return Result.OK(list);
    }


    /**
     * 求职大厅列表查询
     *
     * @param tbUserRole
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "tb_user_role-分页列表查询")
    @ApiOperation(value="求职大厅列表查询", notes="求职大厅列表查询")
    @GetMapping(value = "/rolelist1")
    public Result<Map> rolelist1(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<TbUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role",2).eq("audit_status",1).orderByDesc("create_time");
        Page<TbUserRole> page = new Page<TbUserRole>(pageNo, pageSize);
        IPage<TbUserRole> pageList = tbUserRoleService.page(page, queryWrapper);
        List<TbUserRole> records = pageList.getRecords();
        List<UserRole1Vo> vos = new ArrayList<>();
        for (TbUserRole record : records) {
            UserRole1Vo userRole1Vo = new UserRole1Vo();
            BeanUtils.copyProperties(record,userRole1Vo);
            userRole1Vo.setHeadImage(String.valueOf(redisUtil.get("")));
            vos.add(userRole1Vo);
        }
        //转个vo好对接
        Map<Object, Object> map = new HashMap<>();
        map.put("pages",pageList.getPages());
        map.put("current",pageList.getCurrent());
        map.put("total",pageList.getTotal());
        map.put("size",pageList.getSize());
        map.put("records",vos);
        return Result.OK(map);
    }



    //@AutoLog(value = "tb_task-分页列表查询")
    @ApiOperation(value="t招聘大厅列表查询", notes="招聘大厅列表查询")
    @GetMapping(value = "/taskList")
    public Result<IPage<TbTaskVo>> taskList( @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                           @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                           @RequestParam(name="latitude") String latitude,
                                           @RequestParam(name="longitude") String longitude) {
        Page<TbTaskVo> page = new Page<TbTaskVo>(pageNo, pageSize);
        IPage<TbTaskVo> tasks = TaskMapper.tasks(latitude,longitude , null, null, page);

        return Result.OK(tasks);
    }






}
