package com.jojubanking.boot.module.system.controller.admin.wqdaview;

import com.jojubanking.boot.module.system.dal.dataobject.wq.WqDO;
import com.jojubanking.boot.module.system.dal.dataobject.wqwt.WqwtDO;
import com.jojubanking.boot.module.system.dal.mysql.wq.WqMapper;
import com.jojubanking.boot.module.system.dal.mysql.wqwt.WqwtMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.wqdaview.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wqdaview.WqdaViewDO;
import com.jojubanking.boot.module.system.convert.wqdaview.WqdaViewConvert;
import com.jojubanking.boot.module.system.service.wqdaview.WqdaViewService;

@Api(tags = "管理后台 - VIEW")
@RestController
@RequestMapping("/system/wqda-view")
@Validated
public class WqdaViewController {

    @Resource
    private WqdaViewService wqdaViewService;

    @Resource
    private WqMapper wqMapper;

    @PostMapping("/create")
    @ApiOperation("创建VIEW")
    @PreAuthorize("@ss.hasPermission('system:wqda-view:create')")
    public CommonResult<Integer> createWqdaView(@Valid @RequestBody WqdaViewCreateReqVO createReqVO) {
        return success(wqdaViewService.createWqdaView(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新VIEW")
    @PreAuthorize("@ss.hasPermission('system:wqda-view:update')")
    public CommonResult<Boolean> updateWqdaView(@Valid @RequestBody WqdaViewUpdateReqVO updateReqVO) {
        wqdaViewService.updateWqdaView(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除VIEW")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wqda-view:delete')")
    public CommonResult<Boolean> deleteWqdaView(@RequestParam("id") Integer id) {
        wqdaViewService.deleteWqdaView(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得VIEW")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wqda-view:query')")
    public CommonResult<WqdaViewRespVO> getWqdaView(@RequestParam("id") Integer id) {
        WqdaViewDO wqdaView = wqdaViewService.getWqdaView(id);
        return success(WqdaViewConvert.INSTANCE.convert(wqdaView));
    }

    @GetMapping("/list")
    @ApiOperation("获得VIEW列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:wqda-view:query')")
    public CommonResult<List<WqdaViewRespVO>> getWqdaViewList(@RequestParam("ids") Collection<Integer> ids) {
        List<WqdaViewDO> list = wqdaViewService.getWqdaViewList(ids);
        return success(WqdaViewConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得VIEW分页")
    @PreAuthorize("@ss.hasPermission('system:wqda-view:query')")
    public CommonResult<PageResult<WqdaViewRespVO>> getWqdaViewPage(@Valid WqdaViewPageReqVO pageVO) {
        PageResult<WqdaViewDO> pageResult = wqdaViewService.getWqdaViewPage(pageVO);
        return success(WqdaViewConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出VIEW Excel")
    @PreAuthorize("@ss.hasPermission('system:wqda-view:export')")
    @OperateLog(type = EXPORT)
    public void exportWqdaViewExcel(@Valid WqdaViewExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WqdaViewDO> list = wqdaViewService.getWqdaViewList(exportReqVO);
        // 导出 Excel
        List<WqdaViewExcelVO> datas = WqdaViewConvert.INSTANCE.convertList02(list);

        List<WqdaViewExcel01VO> list1 = new ArrayList<WqdaViewExcel01VO>( datas.size() );
        for ( WqdaViewExcelVO wqdaViewExcelVO : datas ) {

            WqdaViewExcel01VO wqdaViewExcel01VO = new WqdaViewExcel01VO();

            wqdaViewExcel01VO.setId( wqdaViewExcelVO.getId() );

            //questionLabel
            WqDO wqDO = new WqDO();
            wqDO = wqMapper.selectOne("type_id", wqdaViewExcelVO.getTypeId().toString());
            wqdaViewExcel01VO.setTypeId( wqDO.getTitle() );

//            wqdaViewExcel01VO.setQuestionId( wqdaViewDO.getQuestionId() );
//            wqdaViewExcel01VO.setQuestionType( wqdaViewDO.getQuestionType() );
            if(wqdaViewExcelVO.getQuestionTitle()!=null){
                if(wqdaViewExcelVO.getQuestionTitle().equals("其他")){
                    wqdaViewExcel01VO.setQuestionTitle( wqdaViewExcelVO.getQuestionTitle() + ":" +  wqdaViewExcelVO.getQuestionComment());
                }else {
                    wqdaViewExcel01VO.setQuestionTitle( wqdaViewExcelVO.getQuestionTitle() );
                }
            }else {
                wqdaViewExcel01VO.setQuestionTitle( wqdaViewExcelVO.getQuestionTitle() );
            }
//            wqdaViewExcel01VO.setQuestionAnswer( wqdaViewExcelVO.getQuestionAnswer() );
//            wqdaViewExcel01VO.setQuestionComment( wqdaViewDO.getQuestionComment() );
            wqdaViewExcel01VO.setUsername( wqdaViewExcelVO.getUsername() );
            wqdaViewExcel01VO.setUsertel( wqdaViewExcelVO.getUsertel() );
            wqdaViewExcel01VO.setCreateTime( wqdaViewExcelVO.getCreateTime() );
            wqdaViewExcel01VO.setQuestionLabel( wqdaViewExcelVO.getQuestionLabel() );

            list1.add( wqdaViewExcel01VO );
        }


        ExcelUtils.write(response, "VIEW.xls", "数据", WqdaViewExcel01VO.class, list1);
    }

}
