package com.jpsoft.shinestar.modules.business.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.business.entity.FillAttendanceFiles;
import com.jpsoft.shinestar.modules.business.service.FillAttendanceFilesService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/business/fillAttendanceFiles")
@Api(description = "fillAttendanceFiles")
public class FillAttendanceFilesController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FillAttendanceFilesService fillAttendanceFilesService;

    @ApiOperation(value="创建空记录")
    @GetMapping("create")
    public MessageResult<FillAttendanceFiles> create(){
        MessageResult<FillAttendanceFiles> msgResult = new MessageResult<>();

        FillAttendanceFiles fillAttendanceFiles = new FillAttendanceFiles();

        msgResult.setData(fillAttendanceFiles);
        msgResult.setResult(true);

        return msgResult;
    }
    
    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<FillAttendanceFiles> add(@RequestBody FillAttendanceFiles fillAttendanceFiles,@RequestAttribute String subject){
        MessageResult<FillAttendanceFiles> msgResult = new MessageResult<>();

        try {
            fillAttendanceFiles.setId(UUID.randomUUID().toString());
            fillAttendanceFiles.setDelFlag(false);
            fillAttendanceFiles.setCreateBy(subject);
            fillAttendanceFiles.setCreateTime(new Date());
            
            int affectCount = fillAttendanceFilesService.insert(fillAttendanceFiles);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(fillAttendanceFiles);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<FillAttendanceFiles> edit(@PathVariable("id") String id){
        MessageResult<FillAttendanceFiles> msgResult = new MessageResult<>();

        try {
            FillAttendanceFiles fillAttendanceFiles = fillAttendanceFilesService.get(id);

            if (fillAttendanceFiles != null) {
                msgResult.setResult(true);
                msgResult.setData(fillAttendanceFiles);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<FillAttendanceFiles> update(@RequestBody FillAttendanceFiles fillAttendanceFiles,@RequestAttribute String subject){
        MessageResult<FillAttendanceFiles> msgResult = new MessageResult<>();

        try {
            fillAttendanceFiles.setUpdateBy(subject);
            fillAttendanceFiles.setUpdateTime(new Date());
            
            int affectCount = fillAttendanceFilesService.update(fillAttendanceFiles);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(fillAttendanceFiles);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

	@ApiOperation(value="删除")
    @PostMapping("delete/{id}")
    public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            FillAttendanceFiles fillAttendanceFiles = fillAttendanceFilesService.get(id);
            fillAttendanceFiles.setDelFlag(true);
            fillAttendanceFiles.setUpdateBy(subject);
            fillAttendanceFiles.setUpdateTime(new Date());

            int affectCount = fillAttendanceFilesService.update(fillAttendanceFiles);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete")
    public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;

            for (String id : idList) {
                FillAttendanceFiles fillAttendanceFiles = fillAttendanceFilesService.get(id);
                fillAttendanceFiles.setDelFlag(true);
                fillAttendanceFiles.setUpdateBy(subject);
                fillAttendanceFiles.setUpdateTime(new Date());

                affectCount += fillAttendanceFilesService.update(fillAttendanceFiles);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String id,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("id_","asc"));

        if (StringUtils.isNotEmpty(id)) {
            searchParams.put("id","%" + id + "%");
        }

        Page<FillAttendanceFiles> page = fillAttendanceFilesService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public MessageResult<Map> list(
            String fillAttendanceId,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("id_","asc"));

        if (StringUtils.isNotEmpty(fillAttendanceId)) {
            searchParams.put("fillAttendanceId",fillAttendanceId);
        }

        Page<FillAttendanceFiles> page = fillAttendanceFilesService.pageSearch(searchParams,0,9999,false,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
