package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.syzy.domain.BBusiWishUniversities;
import com.ruoyi.syzy.domain.BCustomerUniversities;
import com.ruoyi.syzy.service.IBBusiWishUniversitiesService;
import com.ruoyi.syzy.service.IBCustomerUniversitiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "关注的院校")
@RequestMapping("front/customer/university")
public class FrontCustomerUniversityController extends BaseController {

    @Autowired
    private IBCustomerUniversitiesService customerUniversitiesService;
    @Autowired
    private IBBusiWishUniversitiesService wishUniversitiesService;
    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("list")
    @ApiOperation("01 关注院校列表")
    public TableDataInfo list(@ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        String customerCode = SecurityUtils.getLoginUser().getUser().getCode();
        startPage();
        List<BBusiWishUniversities> arr = wishUniversitiesService.listMyByPage(customerCode);
        //处理院校星级竞争力
        arr.stream().forEach(t -> {
            if(StringUtils.isNotEmpty(t.getStar())){
                t.setStar(dictTypeService.getDictDataByType("university_stars",t.getStar()));
            }
        });
        return getDataTable(arr);
    }

    @GetMapping("remove")
    @ApiOperation("02 移除关注院校")
    public AjaxResult remove(@ApiParam("院校Id") @RequestParam Long universityId) {
        String customerCode = SecurityUtils.getLoginUser().getUser().getCode();
        BBusiWishUniversities universities = wishUniversitiesService.selectBBusiWishUniversitiesById(universityId);
        if (null == universities) {
            return AjaxResult.error("未找到对应id" + universityId);
        }
        BCustomerUniversities upd = new BCustomerUniversities();
        BCustomerUniversities cond = new BCustomerUniversities();
        cond.setCustomerCode(customerCode);
        cond.setUniversityId(universityId);
        cond.setStatus(1L);
        List<BCustomerUniversities> list = customerUniversitiesService.selectBCustomerUniversitiesList(cond);
        list.forEach(t -> {
            upd.setId(t.getId());
            upd.setStatus(0L);
            customerUniversitiesService.updateBCustomerUniversities(upd);
            wishUniversitiesService.updateCollect(universityId, -1);
        });
        return success();
    }

    @GetMapping("add")
    @ApiOperation("03 关注院校")
    public AjaxResult add(@ApiParam("院校id") @RequestParam Long universityId) {
        String customerCode = SecurityUtils.getLoginUser().getUser().getCode();
        BBusiWishUniversities universities = wishUniversitiesService.selectBBusiWishUniversitiesById(universityId);
        if (null == universities) {
            return AjaxResult.error("未找到对应id" + universityId);
        }
        BCustomerUniversities cond = new BCustomerUniversities();
        cond.setCustomerCode(customerCode);
        cond.setUniversityId(universityId);
        List<BCustomerUniversities> list = customerUniversitiesService.selectBCustomerUniversitiesList(cond);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.get(0).getStatus() != 1L) {
                BCustomerUniversities upd = new BCustomerUniversities();
                upd.setId(list.get(0).getId());
                upd.setStatus(1L);
                customerUniversitiesService.updateBCustomerUniversities(upd);
            }
        } else {
            cond.setStatus(1L);
            customerUniversitiesService.insertBCustomerUniversities(cond);
        }
        wishUniversitiesService.updateCollect(universityId, 1);
        return success();
    }

}
