package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.Entity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddBaseStudentDevelopmentDto;
import com.xjrsoft.module.student.dto.BaseStudentDevelopmentPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentDevelopmentDto;
import com.xjrsoft.module.student.entity.BaseStudentDevelopment;
import com.xjrsoft.module.student.service.IBaseStudentDevelopmentService;
import com.xjrsoft.module.student.vo.BaseStudentDevelopmentPageVo;
import com.xjrsoft.module.student.vo.BaseStudentDevelopmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @title: 学生去向登记
* @Author dzx
* @Date: 2024-08-04
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentDevelopment")
@Api(value = "/student"  + "/baseStudentDevelopment",tags = "学生去向登记代码")
@AllArgsConstructor
public class BaseStudentDevelopmentController {


    private final IBaseStudentDevelopmentService developmentService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生去向登记列表(分页)")
    @SaCheckPermission("basestudentdevelopment:detail")
    public RT<PageOutput<BaseStudentDevelopmentPageVo>> page(@Valid BaseStudentDevelopmentPageDto dto){

        MPJLambdaWrapper<BaseStudentDevelopment> queryWrapper = new MPJLambdaWrapper<BaseStudentDevelopment>();
        queryWrapper
                .eq(BaseStudentDevelopment::getBaseStudentGraduateId, dto.getBaseStudentGraduateId())
                    .orderByDesc(BaseStudentDevelopment::getId)
                .select(BaseStudentDevelopment::getId)
                .select(BaseStudentDevelopment.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentDevelopmentPageVo.class).contains(x.getProperty()));
        IPage<BaseStudentDevelopment> page = developmentService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<BaseStudentDevelopmentPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentDevelopmentPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生去向登记信息")
    @SaCheckPermission("basestudentdevelopment:detail")
    public RT<BaseStudentDevelopmentVo> info(@RequestParam Long id){
        BaseStudentDevelopmentVo baseStudentDevelopment = developmentService.getInfoById(id);
        if (baseStudentDevelopment == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(baseStudentDevelopment);
    }


    @PostMapping
    @ApiOperation(value = "新增学生去向登记")
    @SaCheckPermission("basestudentdevelopment:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentDevelopmentDto dto){
        BaseStudentDevelopment baseStudentDevelopment = BeanUtil.toBean(dto, BaseStudentDevelopment.class);
        baseStudentDevelopment.setCreateDate(new Date());

        if(baseStudentDevelopment.getCompanyCoopId() != null){
            String tableName = "company_coop";
            Entity entity = Entity.create(tableName);
            entity.set("id", baseStudentDevelopment.getCompanyCoopId());

            Map<String, Object> objectMap = SqlRunnerAdapter.db().dynamicSelectOne(tableName, entity);
            if(objectMap != null){
                baseStudentDevelopment.setWorkCompany(objectMap.get("company_name").toString());
                baseStudentDevelopment.setCompanyScale(objectMap.get("company_size").toString());
            }
        }
        boolean isSuccess = developmentService.save(baseStudentDevelopment);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生去向登记")
    @SaCheckPermission("basestudentdevelopment:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentDevelopmentDto dto){

        BaseStudentDevelopment baseStudentDevelopment = BeanUtil.toBean(dto, BaseStudentDevelopment.class);
        baseStudentDevelopment.setModifyDate(new Date());
        if(baseStudentDevelopment.getCompanyCoopId() != null){
            String tableName = "company_coop";
            Entity entity = Entity.create(tableName);
            entity.set("id", baseStudentDevelopment.getCompanyCoopId());

            Map<String, Object> objectMap = SqlRunnerAdapter.db().dynamicSelectOne(tableName, entity);
            if(objectMap != null){
                baseStudentDevelopment.setWorkCompany(objectMap.get("company_name").toString());
                baseStudentDevelopment.setCompanyScale(objectMap.get("company_size").toString());
            }
        }
        return RT.ok(developmentService.updateById(baseStudentDevelopment));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生去向登记")
    @SaCheckPermission("basestudentdevelopment:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(developmentService.removeBatchByIds(ids));
    }

}