package com.xiaoniu.subsystem.business.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.common.core.result.R;
import com.xiaoniu.common.core.utils.BeanUtil;
import com.xiaoniu.common.core.utils.poi.ExcelUtil;
import com.xiaoniu.common.log.annotation.Log;
import com.xiaoniu.common.log.enums.BusinessType;
import com.xiaoniu.subsystem.business.domain.*;
import com.xiaoniu.subsystem.business.domain.dto.*;
import com.xiaoniu.subsystem.business.domain.vo.ProfileVO;
import com.xiaoniu.subsystem.business.domain.vo.SatisfactionSurveyVO;
import com.xiaoniu.subsystem.business.domain.vo.SignPageVO;
import com.xiaoniu.subsystem.business.service.ISatisfactionSurveyService;
import com.xiaoniu.subsystem.business.service.ISignService;
import com.xiaoniu.subsystem.system.domain.Unit;
import com.xiaoniu.subsystem.system.service.IUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUnitId;
import static com.xiaoniu.common.security.utils.SecurityUtils.getUserId;

/**
 * <p>
 * 满意度调查表 前端控制器
 * </p>
 *
 * @author haoxuyang
 * @since 2025-01-02 10:54:56
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/satisfactionSurvey")
@Tag(name = "满意度调查表", description = "满意度调查")
public class SatisfactionSurveyController {

    private final IUnitService unitService;
    private final ISatisfactionSurveyService satisfactionSurveyService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "详情", description = "传入Id")
    public R<SatisfactionSurveyVO> detail(@Parameter(description = "主键", required = true) @RequestParam Long surveyId) {
        SatisfactionSurvey detail = satisfactionSurveyService.getById(surveyId);
        return R.data(BeanUtil.copy(detail, SatisfactionSurveyVO.class));
    }

    /**
     * 分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页", description = "传入SatisfactionSurveyPageDTO")
    public R<IPage<SatisfactionSurveyVO>> page(@Validated SatisfactionSurveyPageDTO dto) {
        IPage<SatisfactionSurvey> pages = satisfactionSurveyService.page(new Page<>(dto.getPageNum(),dto.getPageSize()),
                new LambdaQueryWrapper<SatisfactionSurvey>()
                        .orderByDesc(SatisfactionSurvey::getCreateTime)
                        .eq(SatisfactionSurvey::getFlightId,dto.getFlightId())
                        .like(StringUtils.isNotBlank(dto.getFlightName()),SatisfactionSurvey::getFlightName,dto.getFlightName())
                        .like(null != dto.getTrainingSite(),SatisfactionSurvey::getTrainingSite,dto.getTrainingSite())
                        .ge(StringUtils.isNotBlank(dto.getTotalScore1()), SatisfactionSurvey::getTotalScore,dto.getTotalScore1())
                        .le(StringUtils.isNotBlank(dto.getTotalScore2()),SatisfactionSurvey::getTotalScore,dto.getTotalScore2())
                        .eq(StringUtils.isNotBlank(dto.getYear()),SatisfactionSurvey::getYear,dto.getYear())

        );
        IPage<SatisfactionSurveyVO> copy = BeanUtil.copy(pages, SatisfactionSurveyVO.class);
        return R.data(copy);
    }

    /*
     * 新增
     * */
    @PostMapping("/save")
    @ApiOperationSupport(order=4)
    @Operation(summary = "新增",description="传入SingDTO")
    public R save(@Validated @RequestBody SatisfactionSurveyDTO dto){
        Unit unit = unitService.getById(getUnitId());
        SatisfactionSurvey copy = BeanUtil.copy(dto, SatisfactionSurvey.class)
                .setCreateDept(unit.getCreateDept())
                .setCreateUser(getUserId())
                .setCreateTime(new Date());

        //计算合计
        Integer courseArrangementSatisfaction =Integer.valueOf(copy.getCourseArrangementSatisfaction()) ;
        Integer dailySupportSatisfaction = Integer.valueOf(copy.getDailySupportSatisfaction());
        Integer teachingAttitudeSatisfaction =Integer.valueOf(copy.getTeachingAttitudeSatisfaction()) ;
        Integer teachingContentSatisfaction =Integer.valueOf(copy.getTeachingContentSatisfaction()) ;
        Integer teachingMethodSatisfaction = Integer.valueOf(copy.getTeachingMethodSatisfaction());
        Integer total = courseArrangementSatisfaction + dailySupportSatisfaction +teachingAttitudeSatisfaction +teachingContentSatisfaction + teachingMethodSatisfaction;
        String totalScore = total.toString();
        copy.setTotalScore(totalScore);

        return R.data(satisfactionSurveyService.save(copy));
    }

    /*
     * 修改
     * */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "修改",description="传入SatisfactionSurveyDTO")
    public R update(@Validated @RequestBody SatisfactionSurveyDTO dto){
        SatisfactionSurvey copy = BeanUtil.copy(dto, SatisfactionSurvey.class)
                .setUpdateUser(getUserId())
                .setUpdateTime(new Date());
        return R.data(satisfactionSurveyService.updateById(copy));
    }

    /*
     * 删除
     * */
    @PostMapping("/remove")
    @ApiOperationSupport(order=7)
    @Operation(summary = "删除",description = "传入ids")
    public R remove(@Parameter(description = "主键集合",required = true) @RequestParam String ids){

        return R.data(satisfactionSurveyService.removeByIds(Arrays.stream(ids.split(",")).toList()));

    }


    /**
     * 导出
     */
    @GetMapping("/export")
    @ApiOperationSupport(order = 3)
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出", description = "传入SatisfactionSurveyDTO")
    public void export(HttpServletResponse response, @Validated SatisfactionSurveyPageDTO dto) {
        List<SatisfactionSurvey> list = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getIds())) {
            list.addAll(satisfactionSurveyService.list(new LambdaQueryWrapper<SatisfactionSurvey>()
                    .orderByDesc(SatisfactionSurvey::getCreateTime)
                    .in(StringUtils.isNotBlank(dto.getIds()), SatisfactionSurvey::getSurveyId,
                            Arrays.stream(dto.getIds().split(",")).toList())));
        } else {
            list.addAll(satisfactionSurveyService.list(new LambdaQueryWrapper<SatisfactionSurvey>()
                    .orderByDesc(SatisfactionSurvey::getCreateTime)
                    .eq(SatisfactionSurvey::getFlightId,dto.getFlightId())
                    .like(StringUtils.isNotBlank(dto.getFlightName()),SatisfactionSurvey::getFlightName,dto.getFlightName())
                    .like(null != dto.getTrainingSite(),SatisfactionSurvey::getTrainingSite,dto.getTrainingSite())
                    .ge(StringUtils.isNotBlank(dto.getTotalScore1()), SatisfactionSurvey::getTotalScore,dto.getTotalScore1())
                    .le(StringUtils.isNotBlank(dto.getTotalScore2()),SatisfactionSurvey::getTotalScore,dto.getTotalScore2())
                    .eq(StringUtils.isNotBlank(dto.getYear()),SatisfactionSurvey::getYear,dto.getYear()))
            );
        }
        ExcelUtil<SatisfactionSurvey> excelUtil = new ExcelUtil<>(SatisfactionSurvey.class);
        excelUtil.exportExcel(response, list, "满意度调查数据");
    }


    /*
     * 导入
     * */
    @PostMapping("/import")
    @Log(title = "满意度调查导入", businessType = BusinessType.IMPORT)
    @ApiOperationSupport(order = 9)
    public R importData(MultipartFile file,
                        @Parameter(description = "是否支持更新",required = true)
                        @RequestParam boolean updateSupport) throws Exception {
        ExcelUtil<SatisfactionSurvey> excelUtil = new ExcelUtil<>(SatisfactionSurvey.class);
        List<SatisfactionSurvey> list = excelUtil.importExcel(file.getInputStream());
        return R.success(satisfactionSurveyService.importData(list,updateSupport));
    }

    /**
     * 模版导入
     */
    @GetMapping("/importTemplate")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "导入模版下载")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SatisfactionSurvey> util = new ExcelUtil<>(SatisfactionSurvey.class);
        util.importTemplateExcel(response, "满意度调查导入模版");
    }

}
