package com.xjrsoft.module.textbook.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseCourseSubject;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.textbook.dto.AddWfTextbookSubscriptionItemHistoryDto;
import com.xjrsoft.module.textbook.dto.UpdateWfTextbookSubscriptionItemHistoryDto;
import com.xjrsoft.module.textbook.dto.WfTextbookSubscriptionItemHistoryPageDto;
import com.xjrsoft.module.textbook.entity.Textbook;
import com.xjrsoft.module.textbook.entity.WfTextbookSubscriptionItemHistory;
import com.xjrsoft.module.textbook.service.IWfTextbookSubscriptionItemHistoryService;
import com.xjrsoft.module.textbook.vo.WfTextbookSubscriptionItemHistoryPageVo;
import com.xjrsoft.module.textbook.vo.WfTextbookSubscriptionItemHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 教材教辅征订项变更历史
* @Author phoenix
* @Date: 2024-06-20
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/wfTextbookSubscriptionItemHistory")
@Api(value = "/textbook"  + "/wfTextbookSubscriptionItemHistory",tags = "教材教辅征订项变更历史代码")
@AllArgsConstructor
public class WfTextbookSubscriptionItemHistoryController {


    private final IWfTextbookSubscriptionItemHistoryService wfTextbookSubscriptionItemHistoryService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教材教辅征订项变更历史列表(分页)")
    @SaCheckPermission("wftextbooksubscriptionitemhistory:detail")
    public RT<PageOutput<WfTextbookSubscriptionItemHistoryPageVo>> page(@Valid WfTextbookSubscriptionItemHistoryPageDto dto){

        MPJLambdaWrapper<WfTextbookSubscriptionItemHistory> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .select(WfTextbookSubscriptionItemHistory::getId)
                .selectAs(BaseCourseSubject::getName, WfTextbookSubscriptionItemHistoryPageVo::getCourseName)
                .selectAs(XjrUser::getName, WfTextbookSubscriptionItemHistoryPageVo::getCreateUserIdCn)
                .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(WfTextbookSubscriptionItemHistoryPageVo.class).contains(x.getProperty()))
                .select(WfTextbookSubscriptionItemHistory.class, x -> VoToColumnUtil.fieldsToColumns(WfTextbookSubscriptionItemHistoryPageVo.class).contains(x.getProperty()))
                .leftJoin(Textbook.class, Textbook::getId, WfTextbookSubscriptionItemHistory::getTextbookId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType,
                        ext -> ext.selectAs(DictionaryDetail::getName, WfTextbookSubscriptionItemHistoryPageVo::getTextbookTypeCn))
                .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId)
                .leftJoin(XjrUser.class, XjrUser::getId, WfTextbookSubscriptionItemHistory::getCreateUserId)
                .eq(dto.getWfTextbookSubscriptionItemId() != null && dto.getWfTextbookSubscriptionItemId() > 0, WfTextbookSubscriptionItemHistory::getWfTextbookSubscriptionItemId, dto.getWfTextbookSubscriptionItemId())
                .eq(dto.getWfTextbookSubscriptionId() != null && dto.getWfTextbookSubscriptionId() > 0, WfTextbookSubscriptionItemHistory::getWfTextbookSubscriptionId, dto.getWfTextbookSubscriptionId())
                .eq(dto.getTextbookType() != null && !dto.getTextbookType().isEmpty(), Textbook::getTextbookType, dto.getTextbookType())
                .like(dto.getBookName() != null && !dto.getBookName().isEmpty(), Textbook::getBookName, dto.getBookName())
                .like(dto.getCourseSubjectIdCn() != null && !dto.getCourseSubjectIdCn().isEmpty(), BaseCourseSubject::getName, dto.getCourseSubjectIdCn())
                .orderByAsc("t.wf_textbook_subscription_item_id, t.history_version")
                ;
        IPage<WfTextbookSubscriptionItemHistoryPageVo> page = wfTextbookSubscriptionItemHistoryService.selectJoinListPage(ConventPage.getPage(dto), WfTextbookSubscriptionItemHistoryPageVo.class, mpjLambdaWrapper);
        PageOutput<WfTextbookSubscriptionItemHistoryPageVo> pageOutput = ConventPage.getPageOutput(page, WfTextbookSubscriptionItemHistoryPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教材教辅征订项变更历史信息")
    @SaCheckPermission("wftextbooksubscriptionitemhistory:detail")
    public RT<WfTextbookSubscriptionItemHistoryVo> info(@RequestParam Long id){
        WfTextbookSubscriptionItemHistory wfTextbookSubscriptionItemHistory = wfTextbookSubscriptionItemHistoryService.getById(id);
        if (wfTextbookSubscriptionItemHistory == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfTextbookSubscriptionItemHistory, WfTextbookSubscriptionItemHistoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教材教辅征订项变更历史")
    @SaCheckPermission("wftextbooksubscriptionitemhistory:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfTextbookSubscriptionItemHistoryDto dto){
        WfTextbookSubscriptionItemHistory wfTextbookSubscriptionItemHistory = BeanUtil.toBean(dto, WfTextbookSubscriptionItemHistory.class);
        boolean isSuccess = wfTextbookSubscriptionItemHistoryService.save(wfTextbookSubscriptionItemHistory);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教材教辅征订项变更历史")
    @SaCheckPermission("wftextbooksubscriptionitemhistory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfTextbookSubscriptionItemHistoryDto dto){

        WfTextbookSubscriptionItemHistory wfTextbookSubscriptionItemHistory = BeanUtil.toBean(dto, WfTextbookSubscriptionItemHistory.class);
        return RT.ok(wfTextbookSubscriptionItemHistoryService.updateById(wfTextbookSubscriptionItemHistory));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教材教辅征订项变更历史")
    @SaCheckPermission("wftextbooksubscriptionitemhistory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfTextbookSubscriptionItemHistoryService.removeBatchByIds(ids));

    }

}