package com.ruoyi.web.controller.front;

import com.alibaba.fastjson2.JSONObject;
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.sy.domain.SyMajor;
import com.ruoyi.sy.service.ISyMajorService;
import com.ruoyi.syzy.domain.BCustomerMarjors;
import com.ruoyi.syzy.service.IBCustomerMarjorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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/marjors")
public class FrontCustomerMarjorsController extends BaseController {

    @Autowired
    private IBCustomerMarjorsService customerMarjorsService;
    @Autowired
    private ISyMajorService majorService;

    @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<JSONObject> arr = customerMarjorsService.listMyByPage(customerCode);
        return getDataTable(arr);
    }

    @GetMapping("remove")
    @ApiOperation("02 移除关注专业")
    public AjaxResult remove(@ApiParam("数据Id") @RequestParam String code) {
        String customerCode = SecurityUtils.getLoginUser().getUser().getCode();

        SyMajor major = majorService.selectSyMajorByCode(code);
        if(null==major){
            return AjaxResult.error("未找到对应编码"+code);
        }
        Long marjorId = major.getId();

        BCustomerMarjors customerMarjors= new BCustomerMarjors();
        customerMarjors.setCustomerCode(customerCode);
        customerMarjors.setMarjorId(marjorId);
        List<BCustomerMarjors> customerMarjorsList=customerMarjorsService.selectBCustomerMarjorsList(customerMarjors);
        customerMarjorsList.forEach(ll->{
            Long id = ll.getId();
            customerMarjorsService.remove(customerCode, id);
        });

        return success();
    }

    @GetMapping("add")
    @ApiOperation("03 关注专业")
    public AjaxResult add(@ApiParam("专业code") @RequestParam String code) {
        String customerCode = SecurityUtils.getLoginUser().getUser().getCode();
        SyMajor major = majorService.selectSyMajorByCode(code);
        if(null==major){
            return AjaxResult.error("未找到对应编码"+code);
        }
        Long marjorId = major.getId();
        customerMarjorsService.add(customerCode, marjorId);
        return success();
    }

}
